javascript - JS regex skips every other match - Stack Overflow

I'm seeing some strange behavior with a RegExp object in JS. I'm attempting to match a query

I'm seeing some strange behavior with a RegExp object in JS. I'm attempting to match a query string against the beginnings of words for a search autoplete function. While iterating over an array of names and returning the matches, the regex only hits on every other expected match.

var words = [
                "catherine",
                "caterpillar", 
                "nice catch", 
                "fat cat", 
                "catalina"
            ],
            re = new RegExp('\\bcat', 'gi'),
            matches = [],
            results, i;

for (i=0; i<words.length; i++) {
    if (re.exec(words[i])) {
        matches.push(words[i]);
    }
}

console.log(matches);

This code returns ["catherine", "nice catch", "catalina"]. Behavior is the same no matter what order the elements are in. If I re-create this RegExp object in every iteration (e.g. re = new RegExp('\\bcat', 'gi') inside the for loop), it works as expected and returns all the array items, but I'd really rather not have to do that for every pass.

I'm not too familiar with regular expressions - is this a problem with my regex? Did I forget a delimiter or something? Or is it just another JS quirk?

I'm seeing some strange behavior with a RegExp object in JS. I'm attempting to match a query string against the beginnings of words for a search autoplete function. While iterating over an array of names and returning the matches, the regex only hits on every other expected match.

var words = [
                "catherine",
                "caterpillar", 
                "nice catch", 
                "fat cat", 
                "catalina"
            ],
            re = new RegExp('\\bcat', 'gi'),
            matches = [],
            results, i;

for (i=0; i<words.length; i++) {
    if (re.exec(words[i])) {
        matches.push(words[i]);
    }
}

console.log(matches);

This code returns ["catherine", "nice catch", "catalina"]. Behavior is the same no matter what order the elements are in. If I re-create this RegExp object in every iteration (e.g. re = new RegExp('\\bcat', 'gi') inside the for loop), it works as expected and returns all the array items, but I'd really rather not have to do that for every pass.

I'm not too familiar with regular expressions - is this a problem with my regex? Did I forget a delimiter or something? Or is it just another JS quirk?

Share Improve this question asked Aug 21, 2012 at 18:15 arizzitanoarizzitano 1821 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

When you call exec on a RegExp object it maintains a lastIndex property that contains the previous index at which your regex matched the string. The next time you attempt to match using exec it will only start looking at index lastIndex + 1, even if you are searching in a different string.

To prevent this, you can set re.lastIndex to -1 on each iteration of the loop, or just drop the global flag when creating the RegExp.

Javascript quirk. :P

http://www.w3schools./jsref/jsref_regexp_g.asp

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744250543a4565147.html

相关推荐

  • javascript - JS regex skips every other match - Stack Overflow

    I'm seeing some strange behavior with a RegExp object in JS. I'm attempting to match a query

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信