简体   繁体   English

(js)仅用于匹配单词的正则表达式

[英](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 返回true

Your regular expression matches exactly one such character. 您的正则表达式恰好匹配一个这样的字符。

You can add the + modifier to match one or more characters. 您可以添加+修饰符以匹配一个或多个字符。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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