我希望使用regex在多个位置验证字符。
源字符串可以是6-4,4-6,6-4
。
想要验证以下内容
the char at position 0 should be [1-7]
the char at position 1 should be [-]
the char at position 2 should be [1-7]
the char at position 3 should be [,]
the char at position 4 should be [1-7]
the char at position 5 should be [-]
the char at position 6 should be [1-7]
the char at position 7 should be [,]
the char at position 8 should be [1-7]
the char at position 9 should be [-]
the char at position 10 should be [1-7]
如果以上匹配为false,则应返回true。
让我知道如何增强下面来验证多个位置和javascript regex的良好参考。
new RegExp("^.{0}[1-7]").test("6-4,4-4,6-4")
发布于 2019-09-29 16:24:15
源可以是7个字符,也可以是11个字符,并且在末尾没有。
specified by op
您可以使用
^(?:[1-7]-[1-7],){1,2}(?:[1-7]-[1-7])$
const regex = /^(?:[1-7]-[1-7],){1,2}(?:[1-7]-[1-7])$/;
const strs = ['6-4,4-6,6-4', '6-4,6-4', '9-2', '123-1231', '1232', '123#1221', '1-2', '1-2,1-2,1-2,']
strs.forEach(str => {
console.log(str, ' | ', regex.test(str))
})
发布于 2019-09-29 16:20:27
https://stackoverflow.com/questions/58156976
复制相似问题