javascript 名字和姓氏的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9434305/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:39:55  来源:igfitidea点击:

Regex for first and last name

javascriptregex

提问by user977151

I'm trying to make a regexp where it has to match a name like: John Smith. The only rules are first and last name should start with capital letter and has to be at least 2 characters long. Also the last name has a limit of 20 characters maximum and there's a coma or white space between the names. So far I have this:

我正在尝试制作一个正则表达式,它必须匹配一个名称,例如:John Smith。唯一的规则是名字和姓氏应以大写字母开头,并且长度必须至少为 2 个字符。此外,姓氏最多不得超过 20 个字符,并且姓名之间有一个逗号或空格。到目前为止,我有这个:

/[A-Z][a-z]+(\s|,)[A-Z][a-z]{19}/

It doesn't work when I tested it in this site: http://www.regular-expressions.info/javascriptexample.html. I'm not sure what I missed. Any ideas?

当我在这个站点测试它时它不起作用:http: //www.regular-expressions.info/javascriptexample.html。我不确定我错过了什么。有任何想法吗?

回答by Ben Lee

Change the {19}to {1,19}. By itself, {19}means "match exactly 19 of the previous character". {1,19}means "match between 1 and 19 of the previous character".

将 更改{19}{1,19}。就其本身而言,{19}意思是“恰好匹配前一个字符的 19 个”。{1,19}表示“匹配前一个字符的 1 到 19”。

/[A-Z][a-z]+(\s|,)[A-Z][a-z]{1,19}/

UPDATE: People are commenting that this does not meet your requirements. As you described them, it's possibly a naive implementation of your requirements, but it is just your original implementation with the bug fixed. If you are actually looking for names, a less naive implementation might be:

更新:人们评论说这不符合您的要求。正如您所描述的,这可能是您需求的幼稚实现,但这只是您修复了错误的原始实现。如果您实际上是在寻找名称,那么一个不那么幼稚的实现可能是:

/^[A-Z][-'a-zA-Z]+,?\s[A-Z][-'a-zA-Z]{0,19}$/

This will catch names with apostrophes or dashes, allows a space after the comma between the names if they are separated by a comma, and allows for single-letter last names. But as the commenters have pointed out, this still fails to match a bunch of legitimate names and matches stuff that is definitely not name-like.

这将捕获带有撇号或破折号的名称,如果名称之间以逗号分隔,则允许在逗号后使用空格,并允许使用单个字母的姓氏。但正如评论者指出的那样,这仍然无法匹配一堆合法名称,并且匹配绝对不是类似名称的东西。

It also adds anchors ^and $to mean the entire string must match. If you are looking for a substring, you can remove those anchors and add in word boundary checks instead:

它还添加了锚点^$意味着整个字符串必须匹配。如果您正在寻找一个子字符串,您可以删除这些锚点并添加单词边界检查:

/\b[A-Z][-'a-zA-Z]+,?\s[A-Z][-'a-zA-Z]{0,19}\b/

回答by georg

You need {0,19}not just {19}. The latter means "exactly 19 chars".

你需要{0,19}不只是{19}。后者的意思是“正好 19 个字符”。

"John Smith".match(/^[A-Z][a-z]{0,19}[\s,][A-Z][a-z]{0,19}$/)

Of course, this regexp doesn't match many totally valid names like "José Ortega y Gasset" or "Charles de Batz-Castelmore d'Artagnan".

当然,这个正则表达式并不匹配许多完全有效的名字,比如“José Ortega y Gasset”或“Charles de Batz-Castelmore d'Artagnan”。

Depending upon how long acceptable surnames can be, you can replace "{0,19}" with "{1,19}" or "{2,19}". The same applies to first names.

根据可接受的姓氏的长度,您可以将“{0,19}”替换为“{1,19}”或“{2,19}”。这同样适用于名字。

回答by tviscon2

Here are some expressions that might help for more complex names

以下是一些可能有助于更复杂名称的表达式

(^[A-Z][a-z]*$) - A typical First Name or Last Name like Thomas
(^[A-Z][a-z][A-Z][a-z]*$) - names like McDonald
(^[A-Z][a-z]*(-|\s)[A-Z][a-z]*$) - names like Tonya-Smith or Tonya Smith
(^[A-Z]('|')[A-Z][a-z]*$) - names like Tim O'Reilly
(^[a-z]+\s[A-Z][a-z]*$) - names like von Gogh

回答by driangle

/^[A-Z][a-z]+[\s|,][A-Z][a-z]{1,19}$/.test("John Smith") // true

回答by James

This should work for you

这应该适合你

\b[A-Z]+.+[?^ ][A-Z].{1,19}|\b[A-Z]+.+[?^,][A-Z].{1,19}

This starts with the beginning of a word, checks that the first letter is caps, matches the first word up to a white space or comma, then checks to make sure the first letter of the next word is capitalized, and matches everything up to 19 characters after that. Also makes sure each name is 2 or more characters long.

这从单词的开头开始,检查第一个字母是否为大写,匹配第一个单词直到空格或逗号,然后检查以确保下一个单词的第一个字母大写,并匹配最多 19 的所有内容之后的字符。还要确保每个名称的长度为 2 个或更多字符。

回答by Jan

The {19} means that the last name must have exactly 19 lowercase characters after the first uppercase character.

{19} 表示姓氏在第一个大写字符之后必须正好有 19 个小写字符。