长度为 3-5 个字符的字母数字字符串的 JavaScript 正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745112/
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
JavaScript regex for alphanumeric string with length of 3-5 chars
提问by newbie
I need regex for validating alphanumeric String with length of 3-5 chars. I tried following regex found from the web, but it didn't even catch alphanumerics correctly.
我需要正则表达式来验证长度为 3-5 个字符的字母数字字符串。我尝试遵循从网上找到的正则表达式,但它甚至没有正确捕获字母数字。
var myRegxp = /^([a-zA-Z0-9_-]+)$/;
if(myRegxp.test(value) == false)
{
return false;
}
回答by Sachin Shanbhag
add {3,5}
to your expression which means length between 3 to 5
添加{3,5}
到您的表达式中,这意味着长度在 3 到 5 之间
/^([a-zA-Z0-9_-]){3,5}$/
回答by Daniel Gehriger
You'd have to define alphanumerics exactly, but
您必须准确定义字母数字,但是
/^(\w{3,5})$/
Should match any digit/character/_ combination of length 3-5.
应该匹配长度为 3-5 的任何数字/字符/_ 组合。
If you also need the dash, make sure to escape it (add it, like this: :\-
)
如果您还需要破折号,请确保将其转义 ( 添加它,如下所示:\-
)
/^([\w\-]{3,5})$/
Also: the ^
anchormeans that the sequence has to start at the beginning of the line (character string), and the $
that it ends at the end of the line (character string). So your value
string mustn't contain anything else, or it won't match.
另外:^
锚意味着序列必须从行(字符串)的开头开始,并且$
它在行(字符串)的末尾结束。因此,您的value
字符串不得包含任何其他内容,否则将不匹配。
回答by shareef
First this script test the strings N having chars from 3 to 5.
首先,此脚本测试具有 3 到 5 个字符的字符串 N。
For multilanguage (arabic, Ukrainian) you Mustuse this
对于多语言(阿拉伯语,乌克兰语),您必须使用它
var regex = /^([a-zA-Z0-9_-\u0600-\u065f\u066a-\u06EF\u06fa-\u06ff\ufb8a\u067e\u0686\u06af\u0750-\u077f\ufb50-\ufbc1\ufbd3-\ufd3f\ufd50-\ufd8f\ufd92-\ufdc7\ufe70-\ufefc\uFDF0-\uFDFD]+){3,5}$/; regex.test('мшефн');
Other wise the below is for EnglishAlphannumeric only
否则以下仅适用于英文字母数字
/^([a-zA-Z0-9_-]){3,5}$/
P.S the above dose not accept special characters
PS 以上不接受特殊字符
one final thing the above dose not take space as test it will fail if there is space if you want space then add after the 0-9\s
最后一件事上面不占用空间作为测试它会失败如果有空间如果你想要空间然后在 0-9\s 之后添加
\s
And if you want to check lenght of all string add dot .
如果您想检查所有字符串的长度,请添加dot 。
var regex = /^([a-zA-Z0-9\s@,!=%$#&_-\u0600-\u065f\u066a-\u06EF\u06fa-\u06ff\ufb8a\u067e\u0686\u06af\u0750-\u077f\ufb50-\ufbc1\ufbd3-\ufd3f\ufd50-\ufd8f\ufd92-\ufdc7\ufe70-\ufefc\uFDF0-\uFDFD]).{1,30}$/;