简体   繁体   English

正则表达式只允许字母和特殊字符,开头没有空格

[英]regex to allow only alphabets and no special characters, with no space at the beginning

I have tried this: but this isn't working as expected.我试过这个:但这没有按预期工作。 However it restricts user to enter space at the beginning.但是它限制用户在开始时输入空格。 where did I made the mistake?我在哪里犯了错误?

Regex that I have tried to build so far: [^-\\s][a-zA-Z\\s]*$到目前为止我尝试构建的正则表达式: [^-\\s][a-zA-Z\\s]*$

Should Match: priya, Abc Xyz应该匹配:priya, Abc Xyz
Should not match: <spaces>binayak , $&&ay%%aac不应该匹配: <spaces>binayak , $&&ay%%aac

First of all, you missed the beginning of string anchor ^ .首先,您错过了字符串锚点^的开头。

Secondly, the inverse character class [^-\\s] is a good attempt at not allowing spaces, but at the same time it does allow other special characters to be at the beginning of the string, which we do not want.其次,反字符类[^-\\s]是不允许空格的一个很好的尝试,但同时它确实允许其他特殊字符位于字符串的开头,这是我们不想要的。 Instead, we could just something very similar to the second character class, just without the \\s : [a-zA-Z] .相反,我们可以只使用与第二个字符类非常相似的东西,只是没有\\s : [a-zA-Z]

Full regex:完整的正则表达式:

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

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

相关问题 限制特殊字符并允许使用字母 - To restrict Special Characters and allow alphabets 如何限制在输入 texbox 中仅输入空格和特殊字符。 当使用字母输入时,我们需要允许两者 - how to restrict ONLY space and special characters to be entered in the input texbox. We need to allow both when entered with alphabets 正则表达式允许特殊字符 - Regex to allow special characters 正则表达式避免使用数字和特殊字符,并在单词之间留出空格 - Regex avoid numeric and special characters and allow space between words 正则表达式允许特殊字符但不允许前导/尾随空格 - regex to allow special characters but not leading/trailing white space JavaScript RegEx仅允许AlphaNumeric字符和一些特殊字符 - JavaScript RegEx to allow only AlphaNumeric Characters and some special characters 正则表达式仅包含数字和2个特殊字符(仅带空格) - Regex for including only numbers and 2 special characters with space only 正则表达式javascript仅允许数字,字母和下划线 - Regex javascript allow only numbers, alphabets and underscore javascript正则表达式允许数字和特殊字符,但不能只有零 - javascript regex to allow numbers and special characters but not zeroes only 仅允许具有 3-15 个数字长度的特定特殊字符正则表达式 - Allow only specific special characters with 3-15 length of digits regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM