小编Ahm*_*med的帖子

如何使用正则表达式仅匹配给定字符串中的特定字符?

我想要一个特定的值,该值只能是数字并且:

  • 长度应为 11。

  • 第一个数字应该是 0。

  • 第二位数字应该是 1。

  • 第三位数字应该是 0, 1, 2, 5。

  • 然后匹配从第四位到末尾的任何数字。

  • 如果第三位数字是1,则最后两位数字(第10、11)应该相同。

  • 如果第三位是2,则第八位、第九位应该相同。

输入字符串和预期结果。

01012345678          -----> allowed.
0101234a5678         -----> not allowed., letter exists.
01112345688          -----> allowed, 10th, 11st are the same
01112345677          -----> allowed, 10th, 11st are the same
01112345666          -----> allowed, 10th, 11st are the same
01112345689          -----> not allowed..10th, 11st different
01112345-678         -----> not allowed..hyphen exists.
01298765532          -----> allowed..8th, 9th are the same.
01298765732          -----> not allowed, 8th, 9th different.
01298765mm432        -----> not allowed, letter exists. …
Run Code Online (Sandbox Code Playgroud)

javascript regex

21
推荐指数
3
解决办法
1764
查看次数

标签 统计

javascript ×1

regex ×1