25

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.

Example:

Ajax Response (plain-text)

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

User-pattern:

/^Alg/ig.test(responseItem)

RegExp Results should look like:

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

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

My code:

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]
             );
        }
    });
})

Console OUTPUT:

/^Alg/ig => should match each item which starts with Alg

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

/^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

Any suggestions?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
V-Light
  • 3,065
  • 3
  • 25
  • 31
  • 1
    Does executing `/^alg/ig.test("Algo")` literally also give you `false`? – pimvdb Jul 31 '11 at 18:56
  • P.S. The point of whole ting is that I want not only match item(s) from the response, but also be able to collect them. – V-Light Jul 31 '11 at 19:01
  • @pimvdb, no. `/^alg/ig.test("Algo")` returns true – V-Light Jul 31 '11 at 19:05
  • Could you please post the result of `text` if you map it first with this function? `text = text.map(function(v) {var res = [];for(var i=0;i – pimvdb Jul 31 '11 at 19:07
  • Trying printing the length of the strings too, in case, somehow, there are 'invisible' characters. – MRAB Jul 31 '11 at 19:12
  • pimvdb and MRAB: See the screen (http://www.imagebanana.com/view/wd4okkdd/31072011214030.png) – V-Light Jul 31 '11 at 19:43

4 Answers4

68

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

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.

For example:

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

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

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
5

You have to remove the global flag "g".

Darm
  • 5,581
  • 2
  • 20
  • 18
0

try this~

var str = "Aldor\rAleph\rAlgae\rAlgo\rAlgol";
var myregexp = /^alg.*$/img;
var match = myregexp.exec(str);
while (match != null) {
    for (var i = 0; i < match.length; i++) {
        alert(match[i]);
    }
    match = myregexp.exec(str);
}
Monday
  • 1,403
  • 12
  • 10
0

it Was the white space that was not matching it:

try this:

$(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');
    console.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].trim()),
           text[index]
         );
    }
});

})

here is jsfiddle:

http://jsfiddle.net/9Gqw2/

ZeNo
  • 1,648
  • 2
  • 15
  • 28
  • It seems to have the same weird-prob as my script. I just tested your version an get the same result.... – V-Light Jul 31 '11 at 19:15