0

I got regex matching everything in { } brackets, but I want to reverse it, so it does match anything, except text inside { }.

My regex now:

/{.*?}/g
  • text: Blah blah { hello } world { 1010 }.
  • matched: ["{ hello }", "{ 1010 }"]

What I need:

  • text: Blah blah { hello } world { 1010 }.
  • matched: ["Blah blah ", " world "]

Thanks for every suggestion. :)

1 Answers1

1

You can easily get it working with the regex you have using String#split and a filter

String#split can take a regex which will then split your string into an array. filter was due to the empty value at the end because there was a {} at the end of the string.

I set up a demo for you, but I modified the regex to /{[^}\n]*}/g so that it doesn't need the non-greedy quantifier.

const regex = /{[^}\n]*}/g;
const text1 = 'Blah blah { hello } world { 1010 }'

console.log(text1.split(regex).filter(val => val))
Zachary Haber
  • 10,376
  • 1
  • 17
  • 31