DEV Community

Cover image for Why is my regex working intermittently?

Why is my regex working intermittently?

Davide de Paolis on January 13, 2020

also Why is my Regular Expression failing every other time it is called? Why is my RegEx working only the first time but not t...
Collapse
 
freecoderx profile image
FreeCoderX

Thank you very much, let me solve this headache

Collapse
 
miraclehu profile image
MiracleHu

Your post save my life!!!

Collapse
 
dvddpl profile image
Davide de Paolis

glad to hear that!

Collapse
 
kelmore5 profile image
Kyle Elmore

Oh boy I am not a fan of this behavior lol Thanks so much for the article and curing my headache!

Collapse
 
dvddpl profile image
Davide de Paolis

glad it helped! thanks for taking the time to leave a comment

Collapse
 
kuus profile image
kuus

this helped me in solving a stupid bug too, thanks!

Collapse
 
brendanwilding profile image
brendanwilding

Life saver, thanks!

Minor typo in your example (regex2 should be regex?)

const regex = /a/gi // const regex = RegExp('a', 'gi')
regex2.test("abc") // --> true
regex2.test("abc") // --> false
regex2.test("abc") // --> true

Collapse
 
dvddpl profile image
Davide de Paolis

thanks! fixed!

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

the DEV rules are that the #help tag is meant for asking for help,
not for giving advice,
that's #tutorial

I know that's confusing

Collapse
 
dvddpl profile image
Davide de Paolis

I see. Makes sense. good to know.

Just one question though. I did NOT use the specific #help tag. I just listed #debugging #javascript #beginners #regularexpressions - is this rule valid also just for the Title you choose? (because it might lead people into thinking that I am asking for help? I phrased the tilte like that because that is exactly how I would google for it, if I had a problem like that)

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

the rule is for the #help tag
it's a good idea to start from questions that people have, we just need to separate the two

Collapse
 
nicolasromerof profile image
Nicolas

Thx! One hour trying to figure out why my regex wasn't working as expected