Question

I have a custom regular expression attribute which implements IClientValidate so that I can use it with unobtrustive validate.

When I run it I get the following error in FireBug

SyntaxError: Invalid quantifier
match = new RegExp(params).exec(value);

It obviously does not like the regular expression that is passed to it, it is valid in C#. I can't seem to work out what I need to do to get it to be valid in JavaScript.

The regex is

^(?i)([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2})$

Was it helpful?

Solution

The JavaScript regex flavor is extremely limited compared to .NET (C#). One of the many features it doesn't support is inline modifiers of the form (?i)regex or (?i:regex). However, because you're using the new RegExp(params) constructor, you should be able to pass the modifier as the second parameter:

"^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2}$",
"i"

OTHER TIPS

Replace (?i) by i modifier:
var patt=/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]?\s?[0-9][ABD-HJLN-UW-Z]{2})$/i

When i run your regex in this : http://regexpal.com/ I get error on this part : (?i)

Rexegpal is a JavaScript regular expression tester. So if you can get it to work here you can then apply it in your code.

Perhaps you have some example string to try with?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top