Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to split a string by whitespaces, and also keep commas separate?

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?

like image 502
Iter Ator Avatar asked Dec 30 '22 18:12

Iter Ator


2 Answers

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.
like image 88
Wiktor Stribiżew Avatar answered Jan 13 '23 14:01

Wiktor Stribiżew


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)
);
like image 21
The fourth bird Avatar answered Jan 13 '23 14:01

The fourth bird