Quantcast
Channel: Get everything after the dash in a string in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by Steve for Get everything after the dash in a string in JavaScript

$
0
0

For those trying to get everything after the first occurrence:

Something like "Nic K Cage" to "K Cage".

You can use slice to get everything from a certain character. In this case from the first space:

const delim = " "const name = "Nic K Cage"const result = name.split(delim).slice(1).join(delim) // prints: "K Cage"

Or if OP's string had two hyphens:

const text = "sometext-20202-03"// Option 1const opt1 = text.slice(text.indexOf('-')).slice(1) // prints: 20202-03// Option 2const opt2 = text.split('-').slice(1).join("-")     // prints: 20202-03

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>