regex.test()只能在其他时间运行 [英] regex.test() only works every other time
问题描述
test()
在Firefox和Chrome中给我提供了一些问题,但是它在Opera和Safari中完美地工作。 麻烦的代码:
var pattern = /(\ s +(?! \ $ \w +)|( ^(?!\ $ \w +)))/克;
if(pattern.test(String(id).replace(/ \s + OR | AND\s + / g,''))){
searchError(You suck。);
返回1;
}
当您传入空格时,它会每次都会封锁它。当你传入像'$ a b'
之类的东西时,它会在Firefox / Chrome中每隔一段时间工作一次。 WEIRD。
这是RegEx引擎中的一个错误,类似的问题也出现在这里。
从我对这个问题的回答:这是一个错误,正则表达式在ECMAScript 3中实现, 基本是一个
/ regex /
.test()
true
和 false
if everyone should be true
,其他所有的电话成功重置它。
Regex test()
is giving me issues in Firefox and Chrome, yet it works flawlessly in Opera and Safari.
troubled code:
var pattern = /(\s+(?!\$\w+)|(^(?!\$\w+)))/g;
if(pattern.test(String(id).replace(/\s+OR|AND\s+/g, ''))) {
searchError("You suck.");
return 1;
}
When you pass in white space, it blocks it every time. When you pass in something like '$a b'
then it will work every other time in Firefox/Chrome. WEIRD.
It's a bug in the RegEx engine, a similar question with the same issue came up here.
From my answer to that question: It's a bug with the way regexes are implemented in ECMAScript 3, there's a great post on the details here.
The basics are a /regex/
with the g
modifier doesn't reset correctly, so multiple .test()
calls alternate between true
and false
if everyone should be true
, every other calls successfully resets it.
这篇关于regex.test()只能在其他时间运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!