javascript 如何在javascript中为最少一个字符编写正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14576456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write Regular expression for minimum one character in javascript?
提问by Rajasekhar
I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.
我对正则表达式的要求很小,这里我需要至少一个字母,然后是数字和特殊字符。我尝试了以下正则表达式,但没有得到解决方案。
/^[a-zA-Z0-9\-\_\/\s,.]+$/
and
和
/^([a-zA-Z0-9]+)$/
回答by Ja?ck
I need minimum of one letter of Alphabets
我需要至少一个字母
[a-z]+
and followed by numbers and special characters.
后跟数字和特殊字符。
[0-9_\/\s,.-]+
Combined together you would get this:
结合在一起你会得到这个:
/^[a-z]+[0-9_\/\s,.-]+$/i
The /i modifier is added for case insensitive matching of alphabetical characters.
添加 /i 修饰符是为了不区分大小写的字母字符匹配。
回答by elclanrs
Try this regex:
试试这个正则表达式:
/^[a-z][\d_\s,.]+$/i
To clarify what this does:
为了澄清它的作用:
^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive
回答by Explosion Pills
You need the other character selection to be separate. I'm confused as to what "numbers and special characters" means, but try:
您需要将其他字符选择分开。我对“数字和特殊字符”的含义感到困惑,但请尝试:
/^[a-z]+[^a-z]+$/i