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

Need to document that global regexes will lead to weird bugs #1

Open
danmactough opened this issue May 16, 2014 · 2 comments
Open

Need to document that global regexes will lead to weird bugs #1

danmactough opened this issue May 16, 2014 · 2 comments
Labels

Comments

@danmactough
Copy link
Contributor

The matches validator uses RegExp#test(), which means you CANNOT pass global regular expressions, or the validator will not work as expected.

Example:

var re = /^[a-zA-Z0-9_-]+$/gi; // note the "g" for global (not to mention the redundant "i")
var str = 'one';

re.test(str); // true
re.test(str); // false
re.test(str); // true
re.test(str); // false
re.test(str); // true
re.test(str); // false
re.test(str); // true
re.test(str); // false
re.test(str); // true
re.test(str); // false
re.test(str); // true
re.test(str); // false
@cpsubrian
Copy link
Member

Had no idea it worked like this.

@danmactough
Copy link
Contributor Author

Yeah. If you don't reuse the regex (which is our usual m.o.), it doesn't come up. But here, the whole point is to reuse the same regex object. Good times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants