wfbarksdale
wfbarksdale

Reputation: 7606

Why does this regexp throw a javascript error

regexp = new RegExp(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b);

Error:66SyntaxError: Unrecognized token '\'

Upvotes: 1

Views: 1005

Answers (2)

Ștefan
Ștefan

Reputation: 758

I think the regex should be:

/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270775

When calling new RegExp() you must pass the pattern as a string. Enclose it in quotes.

var regexp = new RegExp('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b');

You could also create it as using the special /pattern/ delimited syntax, in which it is not quoted:

var regexp = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/;

Upvotes: 5

Related Questions