Javascript 在这个正则表达式中包含“减号”,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4009817/
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
Include the "minus-sign" into this regular expression, how?
提问by Emil H
I have this regex:
我有这个正则表达式:
var alphaExp = /^[a-zA-Z??????\s]+$/;
This is for a name-field in a form validation.
这是用于表单验证中的名称字段。
I need to make it possible to type names like this "Anna-nicole" (note the minus sign). Currently the minus sign causes error.
我需要能够输入像“Anna-nicole”这样的名字(注意减号)。目前减号会导致错误。
I need to remake this regex so that a minus sign can be included in the name; preferably I should make it so that the name also cannotstart with a minus sign, or end with one. Only minus signs between "words, or names" should be allowed.
我需要重新制作这个正则表达式,以便在名称中包含一个减号;最好我应该这样做,以便名称也不能以减号开头或以一个结尾。只应允许在“单词或名称”之间使用减号。
Does anybody know how to do this?
有人知道怎么做这个吗?
BTW, it is JavaScript.
顺便说一句,它是 JavaScript。
回答by Emil H
You have to escape the minus sign using backslash.
您必须使用反斜杠转义减号。
var alphaExp = /^[a-zA-Z??????\s\-]+$/;
To stop them from using it in the beginning or the end, try something like this:
要阻止他们在开始或结束时使用它,请尝试以下操作:
var alphaExp = /^[a-zA-Z??????\s]+[a-zA-Z??????\s\-]*[a-zA-Z??????\s]+$/;
回答by Jonathan Leffler
In many regex dialects, in a character class, a plain hyphen/minus needs to be first or last:
在许多正则表达式方言中,在字符类中,一个简单的连字符/减号需要在第一个或最后一个:
/^[-a-zA-Z??????\s]+$/
/^[a-zA-Z??????\s-]+$/
Negated character classes:
否定字符类:
/^[^-a-zA-Z??????\s]+$/
/^[^a-zA-Z??????\s-]+$/
With close square bracket too, put the square bracket at the front and the hyphen at the end:
也用右方括号,把方括号放在前面,把连字符放在最后:
/^[]a-zA-Z??????\s-]+$/
And if you need to exclude both close square brackets and hyphens, then:
如果您需要同时排除右方括号和连字符,则:
/^[^]a-zA-Z??????\s-]+$/
For the question, one interpretation might be: you want to insist on alphabetic characters around hyphens, and only want to allow spaces at the start and end, and you might want to allow apostrophes where you allow hyphens, and you want to avoid consecutive hyphens or apostrophes.
对于这个问题,一种解释可能是:您想坚持在连字符周围使用字母字符,并且只想在开头和结尾处允许空格,并且您可能希望在允许连字符的地方允许使用撇号,并且您想避免连续的连字符或撇号。
/^\s*[a-zA-Z??????]*([a-zA-Z??????]+[-'][a-zA-Z??????]+)*\s*$/
Warning: regex formally tested with Perl, not JavaScript.
警告:正则表达式是用 Perl 正式测试的,而不是 JavaScript。
Start of string, zero or more spaces; zero or more alphabetic characters; zero or more sequences of 'one or more alphabetic characters plus hyphen or apostrophe and one or more alphabetic characters', followed by end of string. You could slap an extra set of parentheses after the first \s*
and before the second \s*
to capture the whole name.
字符串开头,零个或多个空格;零个或多个字母字符;零个或多个“一个或多个字母字符加连字符或撇号和一个或多个字母字符”的序列,后跟字符串结尾。您可以在第一个之后\s*
和第二个之前添加一组额外的括号\s*
来捕获整个名称。
For Anna-nicole
, the first alpha term would match Ann
, and the other alpha term would match a-nicole
. For Anonymous
, the first term would match the whole string, the second would be empty. For O'Reilly
, the first term would be empty and the second would match the whole string. Names such as "C--d" and "Who''Me" would be rejected (no repeated hyphen or apostrophe allowed). It would allow Smith-Jones-and-Son
as a name, and Smith-And-O'Reilly
. It won't allow leading or trailing hyphens or apostrophes.
对于Anna-nicole
,第一个 alpha 术语将匹配Ann
,另一个 alpha 术语将匹配a-nicole
。对于Anonymous
,第一项将匹配整个字符串,第二项将为空。对于O'Reilly
,第一个术语将为空,第二个术语将匹配整个字符串。诸如“C--d”和“Who''Me”之类的名称将被拒绝(不允许重复的连字符或撇号)。它将允许Smith-Jones-and-Son
作为名称,并且Smith-And-O'Reilly
. 它不允许前导或尾随连字符或撇号。
If you wanted to allow 'first-name last-name', you'd need two lots of the 'core' of the regex above with \s+
in between. Etc.
如果你想允许'first-name last-name',你需要两个上面正则表达式的'核心',中间\s+
有两个。等等。
回答by The Archetypal Paul
/^[a-zA-Z??????\s]+[a-zA-Z??????\s\-]*[a-zA-Z??????\s\]+$/
should do it (there being a nasty edge case of a single character name, which this won't match. Is a single character name allowed or not?)
应该这样做(有一个令人讨厌的单字符名称的边缘情况,这将不匹配。是否允许使用单字符名称?)
回答by Helge Moulding
This may depend on the implementation. In Javascript the backslash will escape the minus sign in a character class, but in, for example, Oracle, only putting the minus sign first or last in the character class will do the trick.
这可能取决于实现。在 Javascript 中,反斜杠将转义字符类中的减号,但在例如 Oracle 中,仅将减号放在字符类中的第一个或最后一个即可。