简体   繁体   English

正则表达式,只允许字符或空格

[英]Regular expression which allow only characters or space

I need help with regular expression.I need a expression in jquery which allows only character or space between two words. 我需要正则表达式的帮助。我需要一个jquery中的表达式,它只允许两个单词之间的字符或空格。 no double space allowed 没有双重空间

I am using this 我正在使用这个

/^[a-zA-Z]+(-_ [a-zA-Z]+)*/

but it's not working. 但它不起作用。

Example space hello - not allowed 示例空间问候 - 不允许

hello space - not allowed 你好空间 - 不允许

hello space space hello - not allowed 你好空间你好 - 不允许

space hello space - not allowed 太空你好空间 - 不允许

hello1234 - not allowed hello1234 - 不允许

hello space 1234 - not allowed 你好空间1234 - 不允许


hello world-allowed 你好世界允许

hello-allowed 你好,允许

hello how are you-allowed 你好,你怎么允许的

你可以用它

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

I see you already have an accepted answer, but here's another, more concise option: 我看到你已经接受了答案,但这是另一个更简洁的选择:

/^\w+( \w+)*$/

Starts with a word, then followed by any number of words with a single space in front. 从一个单词开始,然后是前面有一个空格的任意数量的单词。

Note that this accepts numbers in words too. 请注意,这也接受单词中的数字。 I don't know if that's what you want. 我不知道这是不是你想要的。

这个RegEx对我有用([A-Za-z] () [A-Za-z] +)

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

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