I am trying to split this string:
bar .foo car.coo
to this array:
['bar',' .foo','car','.coo']
I have been trying variations on this phrase but I just can’t seem to get it right:
str.replace(/([.#:])/g, '$1').split(" ");
The rules for the split is – if there is [.#:] then split – but if there is a whitespace before them (like .foo) then include the white space.
How do I do that?
Since
"car.coo"has to be splitted into two array elements without any character being consumed,.splitis probably not what you want.You can use
str.match, which will form an array with the string parsed by the regexp: