user2770382
user2770382

Reputation: 1

Regular expression validation in javascript failing in IE8

trying to use this

(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)

or

^(AD|ad|Ad)\\([a-zA-Z]+$)

in an attempt to validate for strings like AD\loginid or ad\loginid or Ad\loginid

above regex works fine on the regex testers online.. like http://regexpal.com/ or http://www.regular-expressions.info/javascriptexample.html

but when I incorporate it in the script validations it fails for the below code...

var lanidRegex = new RegExp("(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)");
alert(lanidRegex.test("AD\loginid"));

I have rewritten the regex differently multiple times but to no luck..

Upvotes: 0

Views: 465

Answers (6)

user2770382
user2770382

Reputation: 1

This worked finally

var lanidRegex = new RegExp("^(AD|ad|Ad)\\\\([a-zA-Z]+$)", "gi");
lanidRegex.test("ad\name")

Upvotes: 0

Andriy F.
Andriy F.

Reputation: 2537

You can do it simpler

var lanidRegex = new RegExp("^(AD|ad|Ad)\([a-zA-Z]+)$");
var res = lanidRegex.test("AD\loginid");
window.console && console.log(res);

Upvotes: 0

Lee Meador
Lee Meador

Reputation: 12985

I think you need:

var lanidRegex = new RegExp("(^AD\\\\[a-zA-Z]+$)|(^ad\\\\[a-zA-Z]+$)|(^Ad\\\\[a-zA-Z]+$)");

One backslash to get each backslash into the string and one to tell the regex that the backslash is quoted in the regex.

In the alert line, you only need two of them:

alert(lanidRegex.test("AD\\loginid"));

(Thanks to the other answerers for noticing that.)

FIDDLE

Alternate version works too:

var lanidRegex = /(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)/;

Upvotes: 1

Barmar
Barmar

Reputation: 780843

The problem is that \ serves two purposes in Javascript: it's an escape prefix in strings, and also in regexp. Since you're using a string to initialize your regexp, it's doing the string escape. This results in a single backslash in the regexp, so it's then escaping the square bracket.

Instead, use a regexp literal:

var lanidRegex = /(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)/;
alert(lanidRegex.test("AD\\loginid"));

Notice that you also need to double the backslash in the literal string "AD\\loginid".

Your regexp can be simplified greatly by using the i case-insensitive modifier:

var lanidRegex = /^ad\\[a-z]+$/i;

If you don't want to allow aD at the beginning, it can still be simplified:

var lanidRegex = /^(AD|ad|Ad)\\[a-zA-Z]+$/;

You only need alternatives in the prefix, the rest is common to all variants.

Upvotes: 0

Becuzz
Becuzz

Reputation: 6866

You have some escaping problems. First surround your regex pattern with /, it takes care of having to deal with a bunch of escaping problems in strings.

var lanidRegex = new RegExp(/(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)/);

Second, you need to escape the \ in your test string.

alert(lanidRegex.test("AD\\loginid"));

Upvotes: 0

Joseph Myers
Joseph Myers

Reputation: 6552

You need to use double the amount of backslashes (in your case, quadruple backslashes) when you use new RegExp because the first backslash is used to escape the string, and the second backslash is seen by the regular expression.

Your intention is for the regular expression to match a backslash \, which means that the regular expression engine needs to see an escaped backslash \\, which means that your string needs to contain four backslashes "\\\\".

Upvotes: 2

Related Questions