Javascript正则表达式应该传递.test()但似乎失败了 - 为什么? [重复]

时间:2022-01-29 11:29:08

This question already has an answer here:

这个问题在这里已有答案:

I seem to be getting a consistently strange outcome when testing my regex in javascript.

在javascript中测试我的正则表达式时,我似乎得到了一个奇怪的结果。

Here's my fiddle: http://jsfiddle.net/s5fYf/15/

这是我的小提琴:http://jsfiddle.net/s5fYf/15/

This is taken from a web project I'm building. I pass an array of validation objects into my validation function which iterates through them, validating each rule against the value. If one is false it should stop the loop and return a return object which picks up a message and cssClass from the failed rule.

这是从我正在构建的Web项目中获取的。我将验证对象数组传递给我的验证函数,验证函数遍历它们,根据值验证每个规则。如果一个是假的,它应该停止循环并返回一个返回对象,该对象从失败的规则中获取消息和cssClass。

The problem is the validation method seems to return false even if the regex test passes, which should be impossible! So I feel like I'm missing something key. From the debug output you can see that the regex test that is output passes, but then obviously fails when it is tested in the code. This is inline with what I see in my project where if I omit the debug output the return value basically toggles between true and false.

问题是即使正则表达式测试通过,验证方法似乎也返回false,这应该是不可能的!所以我觉得我错过了一些关键的东西。从调试输出中可以看到输出的正则表达式测试通过,但在代码中测试时显然会失败。这与我在项目中看到的内容一致,如果省略调试输出,返回值基本上在true和false之间切换。

Essentially the /regex/.test(value) function seems to oscillate between true and false which is consistent but not what I was expecting... So, my question is what is causing this bizarre behaviour!?

基本上/regex/.test(value)函数似乎在true和false之间振荡,这是一致的,但不是我所期待的......所以,我的问题是导致这种奇怪行为的原因!?

I have tested my regex outside of the solution and as far as I can see it works.

我已经在解决方案之外测试了我的正则表达式,据我所知它可行。

UPDATE:

更新:

Omitting the 'g' or global flag from my regex solved this issue.

从我的正则表达式中省略'g'或全局标志解决了这个问题。

See the answer below and then this link for a full explanation of the global flag and its pitfalls:

请参阅下面的答案,然后单击此链接以获取有关全局标志及其缺陷的完整说明:

Why RegExp with global flag in Javascript give wrong results?

为什么在Javascript中使用全局标志的RegExp会给出错误的结果?

1 个解决方案

#1


21  

It boils down to the fact that the test method of javascript regular expressions returns a result and moves a pointer on to after the match.

归结为javascript正则表达式的测试方法返回结果并在匹配后移动指针。

So the first call returns true, then the second returns false. Check this example: http://jsfiddle.net/B9aVA/

所以第一个调用返回true,然后第二个调用返回false。查看此示例:http://jsfiddle.net/B9aVA/

var regex = /^.+$/g
var input = "Hello";
console.log(regex.test(input));
console.log(regex.test(input));

Writes

true
false

So your code which calls test twice:

所以你的代码调用了两次测试:

case "regex":
    $(".debug-info").append('<span>Result: ' + validation[i].rule.test(value) + '</span><br />');
     if (!validation[i].rule.test(value))
          returnValue.isValid = false;
     break;

My suggestion is to call test once and store the result in a variable, and use that instead

我的建议是调用一次测试并将结果存储在变量中,然后使用它

case "regex":
    var result = validation[i].rule.test(value);
    $(".debug-info").append('<span>Result: ' + result + '</span><br />');
     if (!result)
          returnValue.isValid = false;
     break;

Moral of the story: Methods can have side effects, that's what distinguishes them from properties.

故事的道德:方法可以产生副作用,这就是它们与属性的区别。

#1


21  

It boils down to the fact that the test method of javascript regular expressions returns a result and moves a pointer on to after the match.

归结为javascript正则表达式的测试方法返回结果并在匹配后移动指针。

So the first call returns true, then the second returns false. Check this example: http://jsfiddle.net/B9aVA/

所以第一个调用返回true,然后第二个调用返回false。查看此示例:http://jsfiddle.net/B9aVA/

var regex = /^.+$/g
var input = "Hello";
console.log(regex.test(input));
console.log(regex.test(input));

Writes

true
false

So your code which calls test twice:

所以你的代码调用了两次测试:

case "regex":
    $(".debug-info").append('<span>Result: ' + validation[i].rule.test(value) + '</span><br />');
     if (!validation[i].rule.test(value))
          returnValue.isValid = false;
     break;

My suggestion is to call test once and store the result in a variable, and use that instead

我的建议是调用一次测试并将结果存储在变量中,然后使用它

case "regex":
    var result = validation[i].rule.test(value);
    $(".debug-info").append('<span>Result: ' + result + '</span><br />');
     if (!result)
          returnValue.isValid = false;
     break;

Moral of the story: Methods can have side effects, that's what distinguishes them from properties.

故事的道德:方法可以产生副作用,这就是它们与属性的区别。