14

I tried different ways to escape the parentheses using regex in JavaScript but I still can't make it work.

This is the string:

"abc(blah (blah) blah()...).def(blah() (blah).. () ...)"

I want this to be detected:

abc().def() 

Using this code, it returns false.

 str.match(/abc\([^)]*\)\.def\([^)]*\)/i);

Can you please tell me why my regex is not working?

RoundOutTooSoon
  • 9,821
  • 8
  • 35
  • 52

2 Answers2

12

This regex will match the string you provided:

(abc\().+(\)\.def\().+(\))

And using backreferences $1$2$3 will produce abc().def()

Or just use this if you don't want the back references:

abc\(.+\)\.def\(.+\)
alan
  • 4,752
  • 21
  • 30
0

K... Here's an Idea...

abc(blah (blah) blah()).def(blah() (blah).blah())

Use regExp like this

var regExp1 = \^([a-z])*\(\ig;

it'll match

abc(

then use

var regExp2 = /\)\./

it'll match the

")." 

in the string..

then split the actual string so that it becomes

def(blah() (blah).blah())

repeat till the regular expression can't find the

regExp2 

and add a closing bracket.. The simplest solution I could think of.. Hope it helps..

LearningDeveloper
  • 638
  • 2
  • 11
  • 23