简体   繁体   English

名称的正则表达式,只接受字母,两个单词之间只接受空格。 名称的开头或结尾不允许有空格

[英]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.

相关问题 仅字母和数字但不包含空格的正则表达式 - Regular expression for only alphabet and number but not space AngularJS - 正则表达式不应以空格开头和结尾,不应包含双引号 (") - AngularJS - regular expression which should not start & end with space and should not consist double quotation (") 仅允许单词和空格的正则表达式 - regular expression for allowing words and spaces only 如何生成不包含任何空格的正则表达式,甚至在字符串的开头或结尾也不包含任何空格 - How to generate Regular Expression that doesn't contain any white space at all and even at the beginning or the end of the string Angular指令名称:只允许小写字母? - Angular directive name: only lower case letters allowed? 澳大利亚移动电话号码正则表达式验证需要在号码之间留出空格 - Australian Mobile number regular expression validation needs to allow space in between number 属性名称中带有空格的AngularJS Orderby - AngularJS Orderby with Space in Property Name 正则表达式不是以两个“WW”或“MM”开头 - Regular Expression not start with two 'WW' or 'MM' 在两个模板化变量angularjs之间添加一个空格 - add a space between two templated variables, angularjs 在所有分辨率的两个图标之间添加空格 - Add space between the two icons for all resolutions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM