question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update Validators.pattern docs to mention the use of global flag in RegExp

See original GitHub issue

We 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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewKushnircommented, Sep 30, 2020

@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 a RegExp 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 a RegExp has a global flag might be too restrictive as well, since there might be use-cases where developers rely on shifting the lastIndex 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.

1reaction
AleksanderBodurricommented, Sep 30, 2020

@AndrewKushnir I can take this issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found