简体   繁体   English

正则表达式-名称中只能包含一个空格和一个连字符

[英]regex - Allow only one space and one hypen in a name

Currently, my regex is allowing for multiple spaces and hyphens but simply not allowing them one after another in a name. 目前,我的正则表达式允许使用多个空格和连字符,但根本不允许名称中一个接一个的空格和连字符。

Currently it allows for multiple spaces and hypens: 当前,它允许多个空格和连字符:

vjbn-bjnlm-bnj-

gvjhb vgbhjk vghj

vgjbh-vgh vghb vghbj-

How would I adjust this to ONLY allow for 1 of space or hypen EACH: 我如何将其调整为仅允许1个空间或hypen每个:

jhbn-vgbh vghjbj

My current regex is: 我当前的正则表达式是:

/^[À-ÿA-Za-z]+(?:[À-ÿA-Za-z]+|([-' ])(?!\1))*/

At the beginning, you can add negative lookahead for .+-.+- , thus excluding strings with more than one dash, and then use the same sort of pattern again to exclude strings with more than one space: 首先,您可以为.+-.+-添加负前行,从而排除具有多个破折号的字符串,然后再次使用相同的模式来排除具有多个空格的字符串:

^(?!.+-.+-)(?!.+ .+ )[À-ÿA-Za-z]+(?:[À-ÿA-Za-z]+|([-' ])(?!\1))*
 ^^^^^^^^^^^^^^^^^^^^

https://regex101.com/r/61kC3C/1 https://regex101.com/r/61kC3C/1

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

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