regex.test()只能在其他时间运行 [英] regex.test() only works every other time

查看:120
本文介绍了regex.test()只能在其他时间运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正则表达式 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 /

code> g 修饰符没有正确重置,所以多个 .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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆