How can I split a string only once, i.e. make 1|Ceci n'est pas une pipe: | Oui
parse to: ["1", "Ceci n'est pas une pipe: | Oui"]
?
The limit in split doesn't seem to help...
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.
You'd want to use String.indexOf('|')
to get the index of the first occurrence of '|'.
var i = s.indexOf('|'); var splits = [s.slice(0,i), s.slice(i+1)];
This isn't a pretty approach, but works with decent efficiency:
var string = "1|Ceci n'est pas une pipe: | Oui"; var components = string.split('|'); alert([components.shift(), components.join('|')]);
Here's a quick demo of it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With