I'm seriously going crazy about this, and I have already spent a disproportionate amount of time trying to figure out what is going on here. So please give me a hand =)
I need to do some RegExp string matching in JavaScript. Unfortunately, this behaves very strangely. This code:
var rx = /(cat|dog)/gi; var w = new Array("I have a cat and a dog too.", "There once was a dog and a cat.", "I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There once was a dog and a cat.","I have a cat and a dog too.", "There once was a dog and a cat."); for (var i in w) { var m = null; m = rx.exec(w[i]); if(m){ document.writeln("<pre>" + i + "\nINPUT: " + w[i] + "\nMATCHES: " + m.slice(1) + "</pre>"); }else{ document.writeln("<pre>" + i + "\n'" + w[i] + "' FAILED.</pre>"); } }
It returns "cat" and "dog" for the first two elements, as it should be, but then some exec() -calls start returning null . I do not understand why.
I posted a fiddle here where you can run and edit code.
And while I tried it in Chrome and Firefox.
Hurrah!
/ Christofer
javascript regex
cpak Jan 18 2018-11-18T00: 00Z
source share