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

209
Views
Regex solution for matching groups does not work

Imagine a text like in this example:

some unimportant content

some unimportant content [["string1",1,2,5,"string2"]] some unimportant content

some unimportant content

I need a REGEX pattern which will match the parts in [[ ]] and I need to match each part individually separated by commas.

I already tried

const regex = /\[\[(([^,]*),?)*\]\]/g
const found = result.match(regex)

but it doesn't work as expected. It matches only the full string and have no group matches. Also it has a catastrophic backtracking according to regex101.com if the sample text is larger.

Output should be a JS array ["string1", 1, 2, 5, "string2"]

Thank you for your suggestions.

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

0

What about going with a simple pattern like /\[\[(.*)\]\]/g and then you'd just have to split the result (and apparently strip those extra quotation marks):

const result = `some unimportant content

some unimportant content [["string1",1,2,5,"string2"]] some unimportant content

some unimportant content`;

// const found = /\[\[(.*)\]\]/g.exec(result);
const found = /\[\[(.*?)\]\]/g.exec(result); // As suggested by MikeM
const arr_from_found = found[1].replace(/\"/g, '').split(',');

console.log(arr_from_found); // [ 'string1', '1', '2', '5', 'string2' ]
about 4 years ago · Juan Pablo Isaza Report

0

Try replace method.

let cleantext = result.replace("[", "")

then

let more_cleantext = cleantext.replace("]", "")

but if your result variable is array then just

result[0]
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!