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
尝试验证AD \ loginid或ad \ loginid或Ad \ _ loginid等字符串
above regex works fine on the regex testers online.. like http://regexpal.com/ or http://www.regular-expressions.info/javascriptexample.html
以上正则表达式在网上正则表达式测试工作正常..如http://regexpal.com/或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..
我已经多次重写了正则表达式,但没有运气..
6 个解决方案
#1
2
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.
当您使用新的RegExp时,需要使用两倍的反斜杠(在您的情况下,四倍反斜杠),因为第一个反斜杠用于转义字符串,第二个反斜杠由正则表达式看到。
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 "\\\\"
.
您的意图是使正则表达式匹配反斜杠\,这意味着正则表达式引擎需要查看转义反斜杠\\,这意味着您的字符串需要包含四个反斜杠“\\\\”。
#2
1
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.)
(感谢其他回答者注意到这一点。)
小提琴
Alternate version works too:
替代版本也适用:
var lanidRegex = /(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)/;
#3
0
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"));
#4
0
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.
问题是\在Javascript中有两个用途:它是字符串中的转义前缀,也是regexp中的转义前缀。由于您正在使用字符串来初始化正则表达式,因此它正在执行字符串转义。这会在regexp中产生一个反斜杠,因此它会转出方括号。
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"
.
请注意,您还需要在文字字符串“AD \\ loginid”中加倍反斜杠。
Your regexp can be simplified greatly by using the i
case-insensitive modifier:
使用i case-insensitive修饰符可以大大简化你的regexp:
var lanidRegex = /^ad\\[a-z]+$/i;
If you don't want to allow aD
at the beginning, it can still be simplified:
如果您不想在开头允许aD,它仍然可以简化:
var lanidRegex = /^(AD|ad|Ad)\\[a-zA-Z]+$/;
You only need alternatives in the prefix, the rest is common to all variants.
您只需要前缀中的替代品,其余的对所有变体都是通用的。
#5
0
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);
#6
0
This worked finally
这终于奏效了
var lanidRegex = new RegExp("^(AD|ad|Ad)\\\\([a-zA-Z]+$)", "gi");
lanidRegex.test("ad\name")
#1
2
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.
当您使用新的RegExp时,需要使用两倍的反斜杠(在您的情况下,四倍反斜杠),因为第一个反斜杠用于转义字符串,第二个反斜杠由正则表达式看到。
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 "\\\\"
.
您的意图是使正则表达式匹配反斜杠\,这意味着正则表达式引擎需要查看转义反斜杠\\,这意味着您的字符串需要包含四个反斜杠“\\\\”。
#2
1
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.)
(感谢其他回答者注意到这一点。)
小提琴
Alternate version works too:
替代版本也适用:
var lanidRegex = /(^AD\\[a-zA-Z]+$)|(^ad\\[a-zA-Z]+$)|(^Ad\\[a-zA-Z]+$)/;
#3
0
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"));
#4
0
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.
问题是\在Javascript中有两个用途:它是字符串中的转义前缀,也是regexp中的转义前缀。由于您正在使用字符串来初始化正则表达式,因此它正在执行字符串转义。这会在regexp中产生一个反斜杠,因此它会转出方括号。
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"
.
请注意,您还需要在文字字符串“AD \\ loginid”中加倍反斜杠。
Your regexp can be simplified greatly by using the i
case-insensitive modifier:
使用i case-insensitive修饰符可以大大简化你的regexp:
var lanidRegex = /^ad\\[a-z]+$/i;
If you don't want to allow aD
at the beginning, it can still be simplified:
如果您不想在开头允许aD,它仍然可以简化:
var lanidRegex = /^(AD|ad|Ad)\\[a-zA-Z]+$/;
You only need alternatives in the prefix, the rest is common to all variants.
您只需要前缀中的替代品,其余的对所有变体都是通用的。
#5
0
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);
#6
0
This worked finally
这终于奏效了
var lanidRegex = new RegExp("^(AD|ad|Ad)\\\\([a-zA-Z]+$)", "gi");
lanidRegex.test("ad\name")