0

My regexp

var EMAIL_REGEXP = /^[\w.+\-!?/:#$%&*();|<>]{3,}@[a-z0-9]+\.[a-z0-9]+(\.?[a-z0-9]+)*$/g;

But when i test in console on some text like "mail@mail.ru" it return true for first test() call and false for second test() call. I think that is en browser error? but nodejs work the same. How can i fix it and why it is happening?

Amit Visodiya
  • 856
  • 6
  • 18
  • @Wiktor I understand the reason you marked this as duplicate, but the thing is op is passing a single email address to a function test(str) which performs the regex operation... so in this case, without seeing his method body not sure whether the global flag is the problem or not – Mustofa Rizwan Nov 16 '16 at 07:11
  • @Maverick_Mrt: JS tag, `/g` in the regex, *it return true for first test() call and false for second test() call* - we have all the evidence of what is going on. – Wiktor Stribiżew Nov 16 '16 at 07:13
  • the reason is a global flag, i have changed it and script work correct – Andrew Gomenyuk Nov 16 '16 at 07:14
  • @WiktorStribiżew Andrew I knew it could have been the reason, but see as you were talking about a test method therefore it could have been possible with global flag too : https://jsfiddle.net/euwdo8c7/2/ as long as the variables are local – Mustofa Rizwan Nov 16 '16 at 07:19

0 Answers0