JavaScript RegExp.test()返回false,即使它应该返回true

编程入门 行业动态 更新时间:2024-10-08 20:28:55
本文介绍了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

本文发布于:2023-11-27 21:33:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使它   RegExp   JavaScript   test   true

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!