Update Validators.pattern docs to mention the use of global flag in RegExp
See original GitHub issueWe should update the Validators.pattern docs to mention possible effects of using the global flag (e.g. /abc/g
) in a RegExp passed as an argument. If the validator function is invoked multiple times the subsequent calls might produce unexpected results due to the lastIndex
being preserved. Additional info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
RegExp.prototype.global - JavaScript - MDN Web Docs
RegExp.prototype.global. The global accessor property indicates whether or not the g flag is used with the regular expression.
Read more >Angular 2 : Validators.pattern() not working - Stack Overflow
When passing the validator expression as a string, you do not use the regex delimiters / / , and cannot pass regex flags....
Read more >Validators - Angular
RegExp objects created with the g or y flags that are passed into Validators.pattern can produce different results on the same input when...
Read more >regexp validator - FormValidation
The HTML attributes are used to set the validator options via the Declarative plugin ... of JavaScript regular expression flags such as g...
Read more >Regular Expression Language - Quick Reference
You can specify options that control how the regular expression engine interprets a regular expression pattern. Many of these options can be ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sonukapoor thanks for the comment. I was thinking about that too, but my understanding is that we should not change the state of the
RegExp
as this is something that a user controls. Using aRegExp
with a global flag for validation is typically undesirable (and not needed in majority of cases), since it has unclear practical purpose in a context of validation: if there is at least one match, the validation will be treated as successful, so if there are more matches (in case a global flag is used) it doesn’t change the validation result - it will still be successful. If there are no matches (which will be true with or without the global flag) - the validation will fail. Throwing an error in case aRegExp
has a global flag might be too restrictive as well, since there might be use-cases where developers rely on shifting thelastIndex
property. So I think adding a paragraph into the docs is a step forward that provides more information to users and do not restrict or enforce certain logic. We can reconsider this behavior in the future in case we get more feedback. Thank you.@AndrewKushnir I can take this issue.