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

172
Views
Split string with ignore special RegEx substring

I need split the string, split character is "," but must skip content in "("...")".

const paramString = "gyro (param1, param2), proximity, Ultra Wideband (UWB) support, compass";

const paramArr = paramString.match(/[^(\)\,]+(\(.+?\))?/g).map( item => item.trim() ); 

In my result is paramArr[2] bad.

[ "gyro (param1, param2)", "proximity", "Ultra Wideband (UWB)", "support", "compass" ]

I need result:

[ "gyro (param1, param2)", "proximity", "Ultra Wideband (UWB) support", "compass" ]

Please advise how update my RegEx.

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

0

You should use negative lookahead. You are basically selecting all commas (with spaces after them to trim them) that are not followed by a ) if there is no ( before it:

const paramString = "gyro (param1, param2), proximity, Ultra Wideband (UWB) support, compass";
const paramArr = paramString.split(/,\s*(?![^(]*\))/);

console.log(paramArr);

NOTE: it won't work for nested parentheses. It will fail in the following scenario:

const paramString = "gyro (param1, (param2)), proximity, Ultra Wideband (UWB) support, compass";
about 4 years ago · Juan Pablo Isaza Report

0

Try below mentioned regx , this will break your string in expected tokens only.

/[\w\ ]+(\([^)]*\))?(\ \w+)?/gi

The regx breaks your string in following tokens .

gyro (param1, param2)
 proximity
 Ultra Wideband (UWB) support
 compass

Please follow the link for live demo of the regx :

https://regex101.com/r/yCYHgw/1

https://regex101.com/r/yCYHgw/2


(updated, regx to work only with non nested )

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!