简体   繁体   中英

(js)regular expression for matching a words only

I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have

/^[\w\D]$/ 

which is not working :/


sorry guys, forgot to mention that will need to exclude all spec characters also.

You seem to want this one :

/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/

It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.

Example :

/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")

returns true

Your regular expression matches exactly one such character.

You can add the + modifier to match one or more characters.

要匹配仅包含字母和空格字符的字符串,可以使用:

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

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