简体   繁体   中英

Regular expression in javascript to match only letters or spaces

I have the following expression to validate a "name":

/^([a-z\s?]{4,120})[^\s]$/i

But I do not know why accepts special characters: Alex@ is a valid match.

It should be invalid because I have not specified that contains special characters.

What you want is a minimum of 4 characters - you're trying to get it to work with is expecting at least 5 characters because of the non-optional [^\\s] character at the end. Moreover, [^\\s] will actually match any character that isn't a space - I'd bet you want to restrict that to being just letters as well?

Try this instead:

^[a-z\s?]{3,119}[a-z]$

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