The string contains a name, where the first part corresponds to the name and the second to the lastname.
The regular expression must verify these formats:
"Surname1 Surname2, Name1 Name2"
"Surname1, Name1 Name2 Surname2"
Invalid strings:
"Surname1 Surname2 Name1 Name2"
"Surname1, Surname2, Name1 Name2"
"Surname1 Surname2 Name1 Name2,"
I try the following: /([\\w][\\,][\\s]{1}\\b)/
, but did not work
I appreciate any help.
您可以使用此正则表达式:
/\b\w+\s+\w+\s*,\s*\w+\s+\w+\b/
If you want to support numbers in the name like your example above.
var validName = function (name) {
return /^\w+ \w+, \w+ \w+$/.test(name);
};
or if any kind of whitespace is allowed between names
validName = function (name) {
return /^\w+\s\w+,\s\w+\s\w+$/.test(name);
},
or if you only want to allow US letters
validName = function (name) {
return /^[A-Za-z]+ [A-Za-z]+, [A-Za-z]+ [A-Za-z]+$/.test(name);
},
I found a solution that verifies both cases:
This is the regular expression: /\\b\\w+\\s*\\w*,\\s\\w+\\s\\w+\\s*\\w*\\s*\\w*\\s*\\w*\\s*\\w*$\\b/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.