对数字列表进行正则表达式测试
regular expression test on list of numbers
var str = "123456";
if (!(/^'s*'d{6,7}'s*$/.test(str)))
{
console.log("no");
} else {
console.log("yes!");
}
当str的长度为6,7或10时,如何将此正则表达式更改为console.log yes 仅 ?
像下面这样改变你的正则表达式,这样它就可以允许6或7或10位数字,并有一个可选的前导和尾随空格。
^'s*(?:'d{10}|'d{6,7})'s*$
演示代码:
> var str = "1234567890";
undefined
> if (!(/^'s*(?:'d{10}|'d{6,7})'s*$/.test(str)))
... {
... console.log("no");
... } else {
... console.log("yes!");
... }
yes!
解释:
^ the beginning of the string
's* whitespace ('n, 'r, 't, 'f, and " ") (0 or
more times)
(?: group, but do not capture:
'd{10} digits (0-9) (10 times)
| OR
'd{6,7} digits (0-9) (between 6 and 7 times)
) end of grouping
's* whitespace ('n, 'r, 't, 'f, and " ") (0 or
more times)
$ before an optional 'n, and the end of the
string
直接使用或 ing。
^'s*('d{6,7}|'d{10})'s*$
演示 模式说明:
^ the beginning of the string
's* whitespace ('n, 'r, 't, 'f, and " ") (0 or more times)
( group and capture to '1:
'd{6,7} digits (0-9) (between 6 and 7 times)
| OR
'd{10} digits (0-9) (10 times)
) end of '1
's* whitespace ('n, 'r, 't, 'f, and " ") (0 or more times)
$ the end of the string
学习更多…
相关文章:
- 从 JavaScript 到 Java 的正则表达式测试
- 使用正则表达式测试字符串中的特定ASCII字符
- 正则表达式测试
- 针对 JavaScript 中的多个正则表达式测试字符串
- 正则表达式测试算法返回 false
- jQuery 正则表达式测试在 .each 错误中
- LxWxH 的正则表达式测试模式
- JavaScript正则表达式测试字符串是否包含特定单词(带变量)
- 如何在使用变量时防止正则表达式测试切换
- 特定正则表达式测试
- 正则表达式测试方法中的“g”如何交替工作
- 正则表达式测试不能正常工作
- 对数字列表进行正则表达式测试
- 正则表达式测试函数不返回相同的引号
- Javascript数字正则表达式测试失败-无法找出原因
- 可以在正则表达式测试器中创建函数,但不能在应用程序中创建
- Javascript正则表达式测试
- Js正则表达式测试除字符串
- Jquery正则表达式测试字符串中的确切单词
- 以重音开头的正则表达式测试