我有一个这样的字符串
[network-traffic:src_port =我想检查一下,它以=、==或!=结尾
我有一个这样的正则表达式
[^=]*={1}刚开始,现在当用ssss===提供它时,它是匹配的,在第一步,我失败了,因为3=也是匹配的,尽管我只需要1到2个相等来匹配
实现上述目标的最佳方式是什么?
发布于 2021-03-05 08:36:32
您可以使用下面的正则表达式^[^=]*(?:={1,2}|!=)$,它按如下方式分解
match the start of the line
match 0 or more chars which are not an =
match 1 or 2 = OR match !=
match the end of the line发布于 2021-03-05 08:35:03
这个怎么样?
function validate(str) {
return /(?<!.*=)([=!])?=$/.test(str)
}
console.log(validate('[network-traffic:src_port =')); // True
console.log(validate('[network-traffic:src_port ==!=')); // True
console.log(validate('ssss=== it')); // False
console.log(validate('ssss=== it===')); // False
https://stackoverflow.com/questions/66485000
复制相似问题