简体   繁体   English

使用正则表达式进行JavaScript姓氏验证

[英]JavaScript surname validation with regex

I need to write validation for surname with polish letters. 我需要用波兰语写出姓氏的验证。

I wrote something like this: 我写了这样的东西:

"^[A-Z][\u0000-\u007F\u0100-\u017F]+([ |-][A-Z][\u0000-\u007F\u0100-\u017F])*$"

where: 哪里:

  • [AZ] : the first letter must be capital [AZ] :第一个字母必须是大写字母
  • [\-\\Ā-\ſ]+ : other letters [\-\\Ā-\ſ]+ :其他信件
  • ([ |-][AZ][\-\\Ā-\ſ])* : space for optional second part of surname with a "-" or space ([ |-][AZ][\-\\Ā-\ſ])* :姓氏的可选第二部分的空格,带有"-"或空格

Everything is fine, but when I type for example: 一切都很好,但是当我输入例如:

"Matt...;'"

I still have a match. 我还有比赛。 How can I "cut" symbols like dots . 我怎样才能像点一样“剪切”符号. and quotation marks? 和引号?

It seems you may use 看来你可能会用

^[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+(?:\s[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+)?\s[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+(?:-[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+)?$

See the regex demo . 请参阅正则表达式演示

It is based on all Polish alphabet letters regex (that excludes V / v , Q / q and X / x from the ASCII letter range) and will match names that: 它基于所有波兰字母正则表达式 (不包括来自ASCII字母范围的V / vQ / qX / x )并且将匹配以下名称:

  • Matches a first name 匹配名字
  • Then may match an optional second first name (after a whitespace) 然后可以匹配可选的第二个名字(在空格之后)
  • A surname 姓氏
  • An optional surname part after a hyphen. 连字符后面的可选姓氏部分。

Details 细节

  • ^ - start of string ^ - 字符串的开头
  • [A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+ - an uppercase Polish letter and 1+ lowercase ones [A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+ - 一个大写的波兰语字母和1个以上的小写字母
  • (?:\\s[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+)? - 1 or 0 occurrences of a whitespace and then an uppercase Polish letter and 1+ lowercase ones - 1或0次出现的空格,然后是大写的波兰语字母和1 +小写的字母
  • \\s - a single whitespace char \\s - 单个空白字符
  • [A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+ - an uppercase Polish letter and 1+ lowercase ones [A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+ - 一个大写的波兰语字母和1个以上的小写字母
  • (?:-[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ][a-pr-uwy-ząćęłńóśźż]+)? - 1 or 0 occurrences of a hyphen and then an uppercase Polish letter and 1+ lowercase ones - 1或0次连字符,然后是大写波兰语字母和1 +小写字母
  • $ - end of string. $ - 结束字符串。

If you plan to support x , q and v in the names, replace a-pr-uwy-z with az and A-PR-UWY-Z with AZ . 如果您计划在名称中支持xqv ,请将a-pr-uwy-z替换为azA-PR-UWY-Z替换为AZ

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

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