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

116
Views
Regex - How to match all brackets that are not inside quotes

I have the following String:

"Hello, don't match this: !{SUM(1,2)}" but match this: !{SUM(1,2)} "And not this: !{MEAN(3,4)}"

I currently have the following regex (\!{(?:\[??[^\[]*?\})) to match all !{}

How could I add to this to match all !{.} that are NOT inside a pair of double quotes?

Not sure if this is even possible since the middle section would technically be between 2 double quotes..

Thanks for any help!!

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

0

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

^(?>".*"|[^!{}])*\K!{.*}

^ - Anchor to the start of the string. This is important because of what's next.

(?> - atomic group. once matched, don't backtrack.

".*" - Consume anything in quotes. Once consumed, the engine won't backtrack for it

| - or

[^!{}] - match anything that isn't the characters we're interested in. These are still part of the atomic group, so anything consumed can't be matched.

)* - Close the atomic group and match the entire group 0 or more times.

\K - reset the pattern. Throw away everything matched up until this point.

!{.*} - What you're looking for.

So because we're starting out anchored, we're going to consume anything that is in quotes, or definitely isn't what we want. It's consumed in a way that won't let the engine backtrack for it. Then once we get to what we're looking for, we throw everything away. This means anything in quotes won't be matched.

Edit:

Thanks to @Nick posting the link to the other thread, I've found there's a much simpler way to go about it that works with javascript:

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

".*"|(!{.*})

".*" match anything in quotes, ungrouped

| or

(!{.*}) match what you're looking for in group 1.

While technically you're matching unwanted strings, you can filter out everything that doesn't return a group 1, leaving you with what you're looking for.

about 4 years ago · Juan Pablo Isaza Report

0

Since atomic groups are not supported in JS, you can try this simple method:

  1. Match everything that is inside qoutes and remove it
  2. Check if the remaining string has the things that you want to match !{.+}

let s = `"Hello, don't match this: !{SUM(1,2)}" but match this: !{SUM(1,2)} "And not this: !{MEAN(3,4)}"`
let pattern = /!{.+}/gm
if (pattern.test(s.replaceAll(/".+?"/gm, ''))) {
console.log("Match")
}
else {
console.log("Not match")
}

Here s.replaceAll(/".+?"/gm, '') gives you: but match this: !{SUM(1,2)}
then you apply the pattern !{.+} to match everything that is between !{ and }, on it.

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!