简体   繁体   English

正则表达式,以避免特殊字符,并且不应以数字开头

[英]Regular expression to avoid special characters and should not start with numbers

What regular expression can I use to not allow special characters and make certain it does not begin with numbers. 我可以使用什么正则表达式来不允许特殊字符,并确保它不能以数字开头。

I'm using /[^a-zA-Z0-9]/ which is filtering out special characters. 我正在使用/[^a-zA-Z0-9]/来过滤掉特殊字符。 How can I make sure it does not start with numbers in regex itself. 如何确定它不是以正则表达式本身的数字开头。

Also i'm using ng-pattern of angular with my input box. 另外我在输入框中使用了ng的ng-pattern

/^[a-zA-Z][a-zA-Z0-9]*$/

This should do it. 这应该做。

Try this if you want to have one or two words. 如果您想输入一个或两个单词,请尝试此操作。

^[a-zA-Z][a-zA-Z0-9]*(?:\s+[a-zA-Z][a-zA-Z0-9]+)?$

This should work for you 这应该为你工作

^[^\W0-9_][^\W_]+$

If Spaces are permitted 如果允许空格

^[^\W0-9_][a-zA-Z0-9\s]+$

If you're not trying to match the whole string, you can remove ^ and $ which match start and end respectively. 如果您不想匹配整个字符串,则可以删除分别匹配开始和结束的^和$。

You can just use this regex: 您可以只使用此正则表达式:

/^[a-zA-Z]/

to make sure your input is only starting with a letter (non-digit and non-special character). 确保您的输入仅以字母(非数字和非特殊字符)开头。

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

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