4

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?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Iter Ator
  • 8,226
  • 20
  • 73
  • 164

2 Answers2

6

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
3

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 chars

console.log(
  "this,is, a test"
  .split(/(,)|\s+/)
  .filter(Boolean)
);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70