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

Bug with regex #699

Closed
NMFES opened this issue Sep 18, 2020 · 7 comments · Fixed by #1154
Closed

Bug with regex #699

NMFES opened this issue Sep 18, 2020 · 7 comments · Fixed by #1154

Comments

@NMFES
Copy link

NMFES commented Sep 18, 2020

JSFiddle: https://jsfiddle.net/awetv6zk/
Video: https://drive.google.com/file/d/1HJl3HgASyAy4Jlp2oxh-hoWQXpDm0uZL/view
Video: https://drive.google.com/file/d/1qzOBIv3uArafG9n0wBQlHbRztFZs2nyt/view

This expression should fail only if we have 3 or more white spaces in the string. And it works correctly in browser console.
Even if we copy/erase/paste some string into the input it still fails or not with 50% chance :[

@dobromir-hristov
Copy link
Contributor

Hey there. I see the issue yeah... Have you tried with a simplified regex? Not sure what could be the issue exactly atm.

@dobromir-hristov
Copy link
Contributor

Interestingly enough, doing just this works:

      reg: v => new RegExp(/^((?!\s{2,}).)*$/ig).test(v),

@dobromir-hristov
Copy link
Contributor

Also does this happen inside a Webpack App or you have only tried in the browser? Like a fiddle or something?

@NMFES
Copy link
Author

NMFES commented Sep 28, 2020

Hi. I have tried with simplified regex and found that the flag "g" is doing this effect. If we remove it - then it works.
But issue with the flag happens only in plugin (Vue, Nuxt, ...). Even for simple expression like /^[a-z]+$/uig
Btw your variant with native usage of regex+test works for me.

@NMFES
Copy link
Author

NMFES commented Sep 28, 2020

Also does this happen inside a Webpack App or you have only tried in the browser? Like a fiddle or something?

Tried in the App, in JSFiddle and in browser. And in browser it works correctly as you could see.

@nurdism
Copy link
Contributor

nurdism commented Jan 22, 2023

running into this aswell, in addition to regex flags, it seems if the regex is is stored in a variable it will also fail.

export const validator = v => new RegExp(/.../ig).test(v) // works

const regex = new RegExp(/.../ig)
export const validator = v => regex.test(v) // fails

@nurdism
Copy link
Contributor

nurdism commented Jan 22, 2023

https://stackoverflow.com/questions/19277254/javascript-regex-test-fails-when-regex-in-variable-during-jasmine-test

test has weird and unhelpful behavior

test called multiple times on the same global regular expression instance will advance past the previous match.

You can get around this by setting RegExp.lastIndex = 0 before each match is attempted, but be wary of this behavior.

http://plnkr.co/edit/cne6He?p=preview

nurdism added a commit to nurdism/vuelidate that referenced this issue Jan 22, 2023
@nurdism nurdism mentioned this issue Jan 22, 2023
7 tasks
dobromir-hristov pushed a commit that referenced this issue Mar 12, 2023
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

Successfully merging a pull request may close this issue.

3 participants