dizpers dizpers - 12 days ago 4x
Javascript Question

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?


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.