JavaScript RegExp.test()返回false,即使它应该返回true [英] JavaScript RegExp.test() returns false, even though it should return true

查看:245
本文介绍了JavaScript RegExp.test()返回false,即使它应该返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我收到AJAX响应(JSON或带换行符的纯文本).应该通过RegEx检查响应的每个项目,以找出其是否与用户定义的模式匹配.

The Prob: I get an AJAX response (JSON or plaintext with line breaks). Each item of the response should be checked via RegEx to find out whether it matches to user-definded patter or not.

示例:

Ajax响应(纯文本)

Ajax Response (plain-text)

"Aldor
Aleph
Algae
Algo
Algol
Alma-0
Alphard
Altran"

用户模式:

/^Alg/ig.test(responseItem)

RegExp结果应如下所示:

RegExp Results should look like:

Aldor   // false
Aleph   // false
Algae   // true
Algo    // true
Algol   // true
Alma-0  // false
Alphard // false
Altran  // false

但是每次我得到不同的结果(有点怪异)时,例如(/^alg/ig.test("Algo") => )

But each time i get different (and kinda weired) results... e.g. (/^alg/ig.test("Algo") => false)

我的代码:

HTML

...
<form>
  <input id="in" />
</form>
<div id="x">
  Aldor
  Aleph
  Algae
  Algo
  Algol
  Alma-0
  Alphard
  Altran
</div><button id="checker">check!</button>
...

JavaScript(jQuery 1.6.2)

$(function(){
    var $checker = $('#checker');

    $checker.click(function(ev){
        var inputFieldVal = $.trim($('#in').val());
        console.log(inputFieldVal); // Alg
        var regExpPattern = '^'+inputFieldVal,
            re = new RegExp(regExpPattern, 'igm');
        onsole.log(re); // /^Al/gim
        // Get text out of div#x
        var text = $('#x').text();
        // Trim and 'convert' to an array...
            text = $.trim(text).split('\n');
        console.log(text); // ["Aldor", "Aleph", "Algae", "Algo", "Algol", "Alma-0", "Alphard", "Altran"]

        for (var index=0, upper=text.length;index<upper;++index) {
            console.log(
               re.test(text[index]),
               text[index]
             );
        }
    });
})

控制台输出:

/^Alg/ig =>应该匹配以Alg

false "Aldor"
false "Aleph"
true "Algae"
false "Algo" //Why ? O.o
true "Algol"
false "Alma-0"
false "Alphard"
false "Altran"

/^Al/ig =>应该匹配每个项目,因为每个项目都以Al

/^Al/ig => should match each item because every item start with Al

true "Aldor"
false "Aleph" //Why ? O.o
true "Algae"
false "Algo" //Why ? O.o
true "Algol"
false "Alma-0" //Why ? O.o
true "Alphard"
false "Altran" //Why ? O.o

有什么建议吗?

推荐答案

这是处理具有全局g标志的模式时exec或test方法显示的常见行为.

This is a common behavior that the the exec or test methods show when you deal with patterns that have the global g flag.

RegExp对象将跟踪找到匹配项的lastIndex,然后在随后的匹配项中,它将从该lastIndex开始而不是从0开始.

The RegExp object will keep track of the lastIndex where a match was found, and then on subsequent matches it will start from that lastIndex instead of starting from 0.

例如:

var re = /^a/g;
console.log(re.test("ab")); // true, lastIndex was 0
console.log(re.test("ab")); // false, lastIndex was 1

从样式中删除g标志,因为您只是在寻找单个匹配项(正在分别测试每一行).

Remove the g flag from your pattern, since you are looking just for a single match (you are testing each line separately).

这篇关于JavaScript RegExp.test()返回false,即使它应该返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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