Glen Little
Glen Little

Reputation: 7128

Regular expression breaking in browsers?

This regular expression seems to break in Chrome, Firefox and IE11...

'abc(def'.match('\((\w*)')

Is there anything wrong with it? Is there a better way to escape the ( character?

If I do it this way, they are happy:

/\((\w*)/.exec('abc(def')

Upvotes: 1

Views: 134

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

It's because your regex must be enclosed between / instead of ':

'abc(def'.match(/\((\w*)/)

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149010

You'll need to escape the \ characters if you construct the regular expression from a string literal:

'abc(def'.match('\\((\\w*)')

Or simply use a regular expression literal:

'abc(def'.match(/\((\w*)/)

Upvotes: 4

Related Questions