Skip to content

Instantly share code, notes, and snippets.

@johndyer
Created March 16, 2013 23:11
Show Gist options
  • Save johndyer/5178734 to your computer and use it in GitHub Desktop.
Save johndyer/5178734 to your computer and use it in GitHub Desktop.
Why RegExp.lastIndex is really, really important
var r = /love/gi,
s = 'i love you',
a = [s,s,s];
for (var i=0,il=a.length; i<il; i++) {
console.log(r.test(a[i]));
}
/*
RESULTS:
true
false -- whaaaaa?
true
*/
for (var i=0,il=a.length; i<il; i++) {
r.lastIndex = 0;
console.log(r.test(a[i]));
}
/*
RESULTS:
true
true -- much better!
true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment