Javascript 在正则表达式字符括号中包含连字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3697202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including a hyphen in a regex character bracket?
提问by ParoX
$.validator.addMethod('AZ09_', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, 'Only letters, numbers, and _-. are allowed');
When I use somehting like test-123it still triggers as if the hyphen is invalid. I tried \-and --
当我使用类似的test-123东西时,它仍然会触发,好像连字符无效。我试着\-和--
回答by Mark Byers
Escaping using \-should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:
转义 using\-应该没问题,但您也可以尝试将其放在字符类的开头或结尾。这应该适合你:
/^[a-zA-Z0-9._-]+$/
回答by Guffa
Escaping the hyphen using \-is the correct way.
使用连字符转义\-是正确的方法。
I have verified that the expression /^[a-zA-Z0-9.\-_]+$/does allow hyphens. You can also use the \wclass to shorten it to /^[\w.\-]+$/.
我已验证该表达式/^[a-zA-Z0-9.\-_]+$/确实允许使用连字符。您还可以使用\w该类将其缩短为/^[\w.\-]+$/.
(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)
(将连字符放在表达式的最后实际上使其不需要转义,因为它不能成为范围的一部分,但是您可能仍然希望养成总是转义它的习惯。)
回答by T.J. Crowder
\-should work to escape the -in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3
\-应该可以-在字符范围内转义。你能引用你测试的内容吗?因为它似乎有效:http: //jsbin.com/odita3
回答by Máthé Endre-Botond
The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.
\- 可能不起作用,因为您使用字符串从服务器传递了整个内容。如果是这种情况,您应该首先转义 \ 以便服务器端程序也可以处理它。
- In a server side string: \\-
- On the client side: \-
- In regex (covers): -
- 在服务器端字符串中:\\-
- 在客户端:\-
- 在正则表达式中(涵盖): -
Or you can simply put at the and of the [] brackets.
或者您可以简单地放在 [] 括号的和。
回答by akhouri
Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
通常-在正则表达式中使用连字符 ( ) 字符,重要的是要注意转义 ( \-) 和不转义 ( -) 连字符之间的区别,因为连字符除了作为字符本身之外,还被解析为在正则表达式中指定范围。
In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/
In the first case, 使用转义的连字符 ( \-),正则表达式将只匹配连字符,如示例所示/^[+\-.]+$/
In the second case, not escaping for example /^[+-.]+$/here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.
In the second case,例如/^[+-.]+$/这里没有转义,因为连字符在加号和点之间,所以它将匹配所有 ASCII 值在 43(加号)和 46(点)之间的字符,所以将包括逗号(ASCII 值 44)作为边-影响。
回答by Radu Simionescu
A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression
匹配连字符的一种更通用的方法是使用连字符和破折号的字符类(“\p{Pd}”不带引号)。如果您正在处理来自不同文化和来源的文本,您可能会发现有更多类型的连字符,而不仅仅是一个字符。您可以将其添加到 [] 表达式中

