2

Here's my code in Chrome console:

var re = new RegExp('^(http(s)?:\/\/)?(www\.)?example.com','g');

And then I test:

re.test('example.com');

Results I get are weird.. After testing RegExp two times I get true and false after. Continuing testing same string I get rotation of true and false. While the first result is correct and it returns true , why the second returns false?

NGix
  • 2,542
  • 8
  • 28
  • 39
  • You need to reset your lastIndex probably http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results – CodingIntrigue Apr 14 '14 at 11:45
  • Unfortunately resetting last index works only in Firefox. But it suggested me not to use global flag.. and it worked!.. – NGix Apr 14 '14 at 11:49
  • `re.lastIndex = 0` should work across the board. Does for me in Chrome - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex – CodingIntrigue Apr 14 '14 at 11:49
  • Btw I thought it's a Chrome bug, not Javascript issue at all. The question can be closed. Thx! – NGix Apr 14 '14 at 11:50
  • FYI `/` is not a special regex character, unless you use it a delimiter. – Robin Apr 14 '14 at 11:52

0 Answers0