Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Regex Match splitting simple words and quotation words by comma

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!