例如我的数据是这样的
AAAG ( 382 TO 385 )( 1729 TO 1732 )( 2405 TO 2408 )( 3759 TO 3762 )
AAAKSKSAL ( 1941 TO 1949 )( 3973 TO 3981 ) AAAKSKSAL
我尝试了下面的脚本
var xa = "AAAKSKSAL ( 1941 TO 1949 )( 3973 TO 3981 )";
var regex = /([^\dTO\s\(\)]+)/g;
var matches = [];
matches = regex.exec(xa);
alert(matches);
我的字符串仅包含一个数据,但它会发出警报 AAAKSKSAL,AAAKSKSAL
。
或者其他
var xa = "AAAKSKSAL ( 1941 TO 1949 )( 3973 TO 3981 ) ( 1941 TO 1949 )( 3973 TO 3981 ) AAAKSKSAL AAAKSKSAL AAAKSKSAL";
它会发出警报AAAKSKSAL,AAAKSKSAL
。但在前面的示例中,我的输入数据包含四个匹配的元素。但结果只有两个。我的正则表达式有什么错误?
但我在 perl 中尝试了相同的概念,效果很好
$s = "AAAKSKSAL( 1941 TO 1949 )( 3973 TO 3981 ) AAAKSKSAL";
@ar = $s=~m/([^\dTO\s\(\)]+)/g;
print @ar
最佳答案
您的正则表达式没问题,但您错误地使用了表达式.exec()。而不是这个:
var matches = [];
matches = regex.exec(xa);
alert(matches);
试试这个:
var matches = [];
while ((m = regex.exec(xa)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
matches.push(m[0]);
}
alert(matches);
关于Javascript 正则表达式否定给出了不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583127/