简体   繁体   中英

regular expression for minimu two words

I want to make a real name check, and should at last 2 normal words (az).

Eg: If input is:

'real name' => true
'real name real and many words' => true
'name' => false

I have this regex but it seems to don't work

^[a-zA-Z]+\s[a-zA-Z]+$

Any help? regexp for minim two words in javascript

You're almost there. Put \\s and the following [a-zA-Z]+ into a capturing or non-capturing group and make them to repeat one or more times by adding a + quantifier next to that group.

^[a-zA-Z]+(?:\s[a-zA-Z]+)+$

Example:

> /^[a-zA-Z]+(?:\s[a-zA-Z]+)+$/.test('real name')
true
> /^[a-zA-Z]+(?:\s[a-zA-Z]+)+$/.test('real name real and many words')
true
> /^[a-zA-Z]+(?:\s[a-zA-Z]+)+$/.test('name')
false

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