Dmitry Belaventsev
Dmitry Belaventsev

Reputation: 6657

Javascript regexp creation error

I have the following JavaScript code:

aw_check_custom_validation['my-phone'] = /^\+[0-9]{1,3}(\s[0-9]{1,6}\s|\([0-9]{1,6}\)|[0-9]{1,6})[0-9\s]{5,8}$/u;

and I receive the following error:

Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'u'

Why the /u flag causes this error?

Upvotes: 1

Views: 11624

Answers (4)

Green
Green

Reputation: 30785

Not sure if it is your case or not, but I had similar issue with Node.js:

SyntaxError: Invalid flags supplied to RegExp constructor 'u'

I fixed it simply by updating Node.js in my Ubuntu server. Previous version was:

$ node -v
v5.1.1

Then I did:

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
   ... long output
$ sudo apt-get install -y nodejs
   ... shorter output
$ node -v
v6.7.0

And no more issues.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

Well, the /u flag is PHP-specific, enabling Unicode support (which your regex isn't using anyway, unless you're planning on matching Unicode whitespace characters with the \s shorthand). Why not just remove it?

For reference, JavaScript only supports the /g (global matching), /i (case-insensitive matching) and /m (multiline, i. e. allow ^ and $ to match at the start and end of each line) modifiers.

Upvotes: 7

Dave Newton
Dave Newton

Reputation: 160181

Because it's not a valid JavaScript regex flag; the valid flags are g (global), i (ignore case), and m (multiline).

Upvotes: 0

Matt
Matt

Reputation: 75307

It's a highly cryptic message that means that the u flag you are passing to the RegEx constructor is invalid. JavaScript supports;

  • g (global match)
  • m ("Treat beginning and end characters (^ and $) as working over multiple lines")
  • i (case insensitive match)

For further info, see the MDC documentation

Upvotes: 2

Related Questions