I would like to break up a sentence into words. If it only contains whitespaces, then .split(/\s+/)
works.
But how is it possible to split by comma as well, and also keep the comma in the resulting array?
I tried something like this, but it does not work:
.split(/(?=,)|(?!=,)|\s/)
Example input:
"this,is, a test"
Expected output:
["this", ",", "is", ",", "a", "test"]
What do I wrong? Is it even possible using regex only?
You can use
console.log(
"this,is, a test".match(/[^\s,]+|,/g)
)
See the regex demo.
The String#match
method with a regex featuring the g
modifier extracts all non-overlapping occurrences of
[^\s,]+
- any one or more chars other than whitespace (\s
) and a comma|
- or,
- a comma.If you want to split on whitespaces and keep the comma's, another option could be to match 1+ whitespace chars or capture the comma in a capturing group to keep it and remove the empty entries.
(,)|\s+
(,)
Capture a comma in group 1 to keep using split|
Or\s+
Match 1 or more whitespace charsconsole.log(
"this,is, a test"
.split(/(,)|\s+/)
.filter(Boolean)
);
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