仅适用于英文和数字字符的 Javascript 正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5231683/
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 regular expression for english & numeric characters only
提问by geevee
What is the easiest way to check if an expression has English or number characters only? with no spaces and no other characters.
检查表达式是否只有英文或数字字符的最简单方法是什么?没有空格也没有其他字符。
p.s - the first character cannot be a number. upper or lower case.
ps - 第一个字符不能是数字。大写或小写。
回答by Grzegorz Gierlik
I would use: /^[A-Za-z][A-Za-z0-9]*$/
. Here are same examples:
我会使用:/^[A-Za-z][A-Za-z0-9]*$/
。以下是相同的示例:
/^[A-Za-z][A-Za-z0-9]*$/.test("expression");
/^[A-Za-z][A-Za-z0-9]*$/.test("EXPRESSION");
/^[A-Za-z][A-Za-z0-9]*$/.test("e123xpression");
/^[A-Za-z][A-Za-z0-9]*$/.test("E123xpression");
/^[A-Za-z][A-Za-z0-9]*$/.test("1expression");
Without boundaries (^
and $
) regexp match any substring too.
没有边界(^
和$
)正则表达式也匹配任何子字符串。
EDIT: Updated invalid expression
编辑:更新了无效的表达式
回答by Radagast the Brown
Easiest:
最简单:
/^[a-z][a-z0-9]*$/i
explanation of the expression:
表达式的解释:
/
- open expression^
- string must start here. Nothing before[a-z]
- find only one character between a to z, including[a-z0-9]*
- find any sequence of characters either between a to z including, or between 0-9 including (the "any sequence" part is the * in the end)$
- string must end here. Nothing after/
- close expressioni
- the expression is case insensitive
/
- 开放式表达^
- 字符串必须从这里开始。以前什么都没有[a-z]
- 只找到 a 到 z 之间的一个字符,包括[a-z0-9]*
- 查找包含 a 到 z 之间或包含 0-9 之间的任何字符序列(“任何序列”部分是最后的 *)$
- 字符串必须在这里结束。之后什么都没有/
- 亲密的表达i
- 表达式不区分大小写
tested with the following cases
在以下情况下进行测试
var tests = //key = case, value = expected results
{ "joe" : true //only lower case
, "JOE" : true //only capital
, "charsAndCaps" : true //mixed case
, "ABC444" : true //caps and numbers
, "AAaaAA3276" : true //mixed case with numbers
, "111Joe" : false //starts with number
, "112345" : false //only numbers
, "asaaa$" : false //non-alphanumeric char in the end
, "asaaa??" : false //non-latin char in the end
, "asaaнет" : false //non-latin char in the end
, "#asaaa" : false //non-alphanumeric char in the start
, "??asaaa" : false //non-latin char in the start
, "нетasaa" : false //non-latin char in the start
, "aa??asaa" : false //non-latin char in the middle
, "sssнетaa" : false //non-latin char in the middle
, "as&&aaa" : false //non-alphanumeric char in the middle
, "" : false //empty string
}
var tests = //key = case, value = expected results
{ "joe" : true //only lower case
, "JOE" : true //only capital
, "charsAndCaps" : true //mixed case
, "ABC444" : true //caps and numbers
, "AAaaAA3276" : true //mixed case with numbers
, "111Joe" : false //starts with number
, "112345" : false //only numbers
, "asaaa$" : false //non-alphanumeric char in the end
, "asaaa??" : false //non-latin char in the end
, "asaaнет" : false //non-latin char in the end
, "#asaaa" : false //non-alphanumeric char in the start
, "??asaaa" : false //non-latin char in the start
, "нетasaa" : false //non-latin char in the start
, "aa??asaa" : false //non-latin char in the middle
, "sssнетaa" : false //non-latin char in the middle
, "as&&aaa" : false //non-alphanumeric char in the middle
, "" : false //empty string
}
try it at: http://jsfiddle.net/erJ4H/161/
回答by Maxym
try this one:
试试这个:
/^[a-z][a-z\d]*$/i
add some examples:
添加一些示例:
/^[a-z][a-z\d]*$/i.test("check#$#"); // false
/^[a-z][a-z\d]*$/i.test("1check"); // false
/^[a-z][a-z\d]*$/i.test("check1"); // true
/^[a-z][a-z\d]*$/i.test("cHEck1"); // true
回答by Aaron Digulla
Use this: [a-zA-Z][a-zA-Z0-9]*
用这个: [a-zA-Z][a-zA-Z0-9]*
回答by MohammadSoori
/^[A-Za-z\d]+$
/^[A-Za-z\d]+$
/^[A-Za-z\d]+$.test("TEST1"); // true
/^[A-Za-z\d]+$.test("Test2"); // true
/^[A-Za-z\d]+$.test("test3"); // true
/^[A-Za-z\d]+$.test("4TEST"); // true
/^[A-Za-z\d]+$.test("5Test"); // true
/^[A-Za-z\d]+$.test("6test"); // true
/^[A-Za-z\d]+$.test("TE7ST"); // true
/^[A-Za-z\d]+$.test("Te8st"); // true
/^[A-Za-z\d]+$.test("te9st"); // true
回答by diEcho
try this /^[a-z]+[a-z0-9]*$/i
尝试这个 /^[a-z]+[a-z0-9]*$/i