[英]Regular expression for name which accepts alphabets only and space only between two words. no space allowed at the start or at end of the name
I tried this but it is not working我试过了,但它不工作
/^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/
A solution could be ^[a-zA-Z]*?[a-zA-Z]*$
一个解决方案可能是
^[a-zA-Z]*?[a-zA-Z]*$
It checks if it starts with a letter, it has an optional space, and checks if it ends with a letter它检查是否以字母开头,是否有可选空格,并检查是否以字母结尾
You can use this regex, which solve your problem:您可以使用此正则表达式,它可以解决您的问题:
/^(?:[A-Za-z]+ ?[A-Za-z]+)*$/gm
So, your words are only alphabets and can be separated by only one space.因此,您的单词只是字母,只能用一个空格分隔。
If you want the words to be separated by several spaces, you can do the following:如果您希望单词被多个空格分隔,您可以执行以下操作:
/^(?:[A-Za-z]+ *[A-Za-z]+)*$/gm
I use no-capturing groups beacuse of repeated group ( (...)*
).由于重复组(
(...)*
),我使用无捕获组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.