Home > Net >  Regex Match splitting simple words and quotation words by comma
Regex Match splitting simple words and quotation words by comma

Time:07-10

im new to regular expressions and i want to build an expression that finds the below pattern:

I have the string:

"'Hello world',dude, 'Somethings, never, turn and go', bye" 

I want a regular expression that gives this result:

['Hello world',dude,'Somethings, never, turn and go',bye]

Basically, splitting the string on comma but keeping the phrases with quotes that have comma as a whole.

I have this regex:

let s = "'Hello world',dude, 'Somethings, never, turn and go', bye";
let arr = s.split(/(?<=')\s*,\s*|\s*,\s*(?=')/g);
console.log(arr)

If i add this string "'Hello world',dude, Somethings, never, turn and go, bye" it gives this result ["'Hello world'", 'dude, Somethings, never, turn and go, bye'] which is wrong. It doesnt split the other values separated by comma. How do i fix that?

CodePudding user response:

You can use split with a capture group to keep the split values for '...' and using an alternation to split on , between optional whitespace chars.

As split can give empty items in the resulting array, you can remove those with filter(Boolean);

Also matching escaped single quotes:

('[^'\\]*(?:\\.[^'\\]*)*')|\s*,\s*

See a regex demo.

let s = "'Hello world',dude, 'Somethings, never, turn and go', bye";
let arr = s.split(/('[^'\\]*(?:\\.[^'\\]*)*')|\s*,\s*/g).filter(Boolean);
console.log(arr)

CodePudding user response:

I seen your previous question. Coincidentally it showed comma's right after or before the single quote. Hence the "wrong" answer back there. In this case, maybe look for balanced single quotes ahead:

let s = "'Hello world',dude, Somethings, never, turn and go, bye";
let arr = s.split(/\s*,(?=(?:[^']*'[^']*')*[^']*$)\s*/);
console.log(arr)

See an online demo.

  • Related