split string only on first instance of specified character
"good_luck_buddy".split(/_(.+)/)[1]"luck_buddy"
They are defined as
If
separator
contains capturing parentheses, matched results are returned in the array.
So in this case we want to split at _.+
(i.e. split separator being a sub string starting with _
) but also let the result contain some part of our separator (i.e. everything after _
).
In this example our separator (matching _(.+)
) is _luck_buddy
and the captured group (within the separator) is lucky_buddy
. Without the capturing parenthesis the luck_buddy
(matching .+
) would've not been included in the result array as it is the case with simple split
that separators are not included in the result.
What do you need regular expressions and arrays for?
myString = myString.substring(myString.indexOf('_')+1)
var myString= "hello_there_how_are_you"myString = myString.substring(myString.indexOf('_')+1)console.log(myString)
I avoid RegExp at all costs. Here is another thing you can do:
"good_luck_buddy".split('_').slice(1).join('_')