Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning false when calling test() multiple times #74

Closed
cybernetics opened this issue Feb 10, 2014 · 7 comments
Closed

returning false when calling test() multiple times #74

cybernetics opened this issue Feb 10, 2014 · 7 comments

Comments

@cybernetics
Copy link

returning false for the given example test on Firefox Nightly (v30) & Chrome 32.0.1700.107
ff30-test-fail

@cybernetics
Copy link
Author

chrome-version
chrome32-test-fail

@cybernetics
Copy link
Author

but when the generated regex is being validated with some other tools, it's returning true for both example test cases, see below.

generated-regex-is-returning-true

@mihai-vlc
Copy link
Contributor

Thank you for your screens this was already reported and discussed in #63
remove the g modifier and it will work as expected

@cybernetics
Copy link
Author

as per http://blog.stevenlevithan.com/archives/es3-regexes-broken
this bug was in ES3 and expected to get fixed in ES4. the above screens are running latest firefox & chrome and I believe both support ES 5.1 spec. I think this library should take care of this nasty bug, and do not force the end user to think for tweaks, if not yet fixed in todays latest browsers.

thnx for the input.

@cybernetics
Copy link
Author

this library is supposed to provide the abstractions for the low level regex intricacies.

@mihai-vlc
Copy link
Contributor

Well this is more then an year old but you know what they say better late then never 😄 .

This is the expected behavior for a regex with the g flag.
When test() is called on a regular expression with the g flag it sets the lastIndex property of the regular expression object to the character position immediately following the matched sub-string.
When we call it the second time it begins the search from the specified lastIndex. If no match is found it will return false and the lastIndex property is set back to 0.

Here we have some code to better understand this:

var reg = /fn/g;
reg.test('hello') // => false
reg.lastIndex // => 0
reg.test('some fn text') // => true
reg.lastIndex // => 7
reg.test('some fn text') // => false
reg.lastIndex // => 0
reg.test('some fn text fn me me') // => true
reg.test('some fn text fn me me') // => true
reg.test('some fn text fn me me') // => false

If you need to prevent this you can set the lastIndex property back to 0 at any time.

MDN RegExp.lastIndex.

@mihai-vlc
Copy link
Contributor

Closing this one as this is the expected behavior.

@mihai-vlc mihai-vlc changed the title returning false for the given example test on Firefox Nightly (v30) & Chrome 32.0.1700.107 returning false when calling test() multiple times Aug 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants