How do I split a string like this
"please help me "
so that I get an array like this:
["please ","help ","me "]
In other words,I get an array which preserves the space (or spaces)
Thanks
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.
JavaScript String split() Method. JavaScript str. split() method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. separator: It is used to specify the character, or the regular expression, to use for splitting the string.
something like :
var str = "please help me ";
var split = str.split(/(\S+\s+)/).filter(function(n) {return n});
FIDDLE
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