简体   繁体   English

在这个正则表达式中包含“减号”,如何?

[英]Include the “minus-sign” into this regular expression, how?

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).我需要能够输入像“Anna-nicole”这样的名字(注意减号)。 Currently the minus sign causes error.目前减号会导致错误。

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 cannot start 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。

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]+$/;

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 .对于Anna-nicole ,第一个 alpha 术语将匹配Ann ,另一个 alpha 术语将匹配a-nicole For Anonymous , the first term would match the whole string, the second would be empty.对于Anonymous ,第一个术语将匹配整个字符串,第二个将是空的。 For O'Reilly , the first term would be empty and the second would match the whole string.对于O'Reilly ,第一个术语将为空,第二个术语将匹配整个字符串。 Names such as "C--d" and "Who''Me" would be rejected (no repeated hyphen or apostrophe allowed).诸如“C--d”和“Who''Me”之类的名称将被拒绝(不允许重复的连字符或撇号)。 It would allow Smith-Jones-and-Son as a name, and Smith-And-O'Reilly .它将允许Smith-Jones-and-SonSmith-And-O'Reilly作为名称。 It won't allow leading or trailing hyphens or apostrophes.它不允许前导或尾随连字符或撇号。

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.如果你想允许'first-name last-name',你需要两批上面正则表达式的'核心',中间有\\s+ Etc.

/^[a-zA-ZåäöÅÄÖ\s]+[a-zA-ZåäöÅÄÖ\s\-]*[a-zA-ZåäöÅÄÖ\s\]+$/ 

应该这样做(有一个令人讨厌的单字符名称的边缘情况,这将不匹配。是否允许使用单字符名称?)

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 中,仅将减号放在字符类中的第一个或最后一个即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM