Javascript String.Match IE Error

The following javascript code combines the matches found by the regular expression. A regular expression should find any word or set of quoted words. It works fine in FireFox and Chrome, but it doesn't work correctly in IE (I tested it only on IE8).

var searchString = '';
var notString = 'dog cat "pirate ship"';
var matches = notString.match(/(\w+|"[^"]+")/ig);
for (i in matches) {
    searchString += " NOT " + matches[i];

}
alert(searchString );

The correct conclusion should be:

NOT a dog NOT a cat NOT a "pirate ship"

but in IE8 im it turns out:

NOT a dog cat "pirate ship" NOT a dog NOT cat NOT "pirate ship" NOT 8 NOT 21

Any suggestions on how to make this cross browser compatible.

Many thanks,

+3
source share
2 answers

for...in. for...in , - :

matches.input
matches.0
matches.1
matches.2
matches.index
matches.lastIndex

. http://msdn.microsoft.com/en-us/library/7df7sf4x(VS.85).aspx:

, , : , lastIndex. . index . lastIndex , .

:

for (var i = 0; i < matches.length; i++)
    searchString += " NOT " + matches[i];
+5

, :

for (var i = 0; i < matches.length; ++i) { /* ... */ }

"in" "for" .

AndyE, , .

+3

Source: https://habr.com/ru/post/1740952/


All Articles