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

Make the global flag optional #8

Closed
kudapara opened this issue Jul 1, 2020 · 1 comment · Fixed by #9
Closed

Make the global flag optional #8

kudapara opened this issue Jul 1, 2020 · 1 comment · Fixed by #9
Assignees
Labels
discussion Issues under discussion documentation Improvements or additions to documentation enhancement New feature or request

Comments

@kudapara
Copy link

kudapara commented Jul 1, 2020

There are scenarios when the SimpleRegex tests will be run multiple times, for example, testing phone numbers during typing, after a user blurs input and just before sending an HTTP request to the server.

In these scenarios, the regex test return alternating results for the exact same phone number. After doing some digging (thanks to stack overflow), I realized that the global regex search starts from the previous match for each invocation of .test and that causes subsequent tests to have a different result even with the same input.

Example: Running SimpleRegex.MobileNumber.Econet.test(phoneNumber) twice, gives you different results

I would like to have an option to remove the global matching of the regexes.

@kudapara kudapara added the enhancement New feature or request label Jul 1, 2020
@michaeldera
Copy link
Owner

Thank you @kudapara. I really ought to have considered this in terms of the documentation bit. So that behaviour is becuase I added the global flag on the Regex pattern. The flag is useful for cases where you want to search for multiple occurrences of a pattern within a large string. More on that here

So my thoughts are that we probably forgo the global flag altogether and allow package users to be able to add it if they are doing search on a paragraph. Maybe something like this:

const regexWithGlobalFlag = RegExp( SimpleRegex.MobileNumber +'/gm' ).... the new regexWithGlobalFlag could then be used to do searches where all occurrences of the string need to be found.

I think this would keep the package small and simple while providing convenience for users but I am open to hearing from people using the package and see what they prefer so I will invite people to discuss this.

@michaeldera michaeldera added documentation Improvements or additions to documentation discussion Issues under discussion labels Jul 2, 2020
@michaeldera michaeldera changed the title Make it optional to globally test the phone numbers Make the global flag optional Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Issues under discussion documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants