我需要使用正则表达式验证一些输入.
以下是一些示例用例和预期结果.
0001 - Matched
001 - Matched
01 - Matched
00.12312 - Matched
000.1232 - Matched
1 - Not matched
20 - Not matched
0.1 - Not matched
0.123123 - Not matched
Run Code Online (Sandbox Code Playgroud)
像这样的正则表达式会是什么样的?如果第一个char是0第二个char,numerical[0-9]则它无效.
我试过这个,但它不起作用.
[0][0-9]
Run Code Online (Sandbox Code Playgroud)
acd*_*ior 11
试试这个正则表达式:
^0[0-9].*$
Run Code Online (Sandbox Code Playgroud)
它将匹配以后0跟数字开头的任何内容.
它将"匹配"你所谓的"无效".
测试代码应使其更清晰:
var regExp = /^0[0-9].*$/
console.log(regExp.test("0001")); // true
console.log(regExp.test("001")); // true
console.log(regExp.test("01")); // true
console.log(regExp.test("00.12312")); // true
console.log(regExp.test("000.1232")); // true
console.log(regExp.test("1")); // false
console.log(regExp.test("20")); // false
console.log(regExp.test("0.1")); // false
console.log(regExp.test("0.123123")); // false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23448 次 |
| 最近记录: |