简体   繁体   中英

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]*$

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 ( (...)* ).

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.

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