0

I have a regex check which fails every second time that it is called.

const PHONE_REGEX = /^(0{0,2}|\+|[1-9])[1-9]\d{7,}$/g;
const validatePhoneNumber = (telNumber) => PHONE_REGEX.test(telNumber);

const input = "01695233966";

[ validatePhoneNumber(input), validatePhoneNumber(input), validatePhoneNumber(input), validatePhoneNumber(input) ]

I have tried replacing validatePhoneNumber calls with just PHONE_REGEX.test but I still experience the same issue.

James Monger
  • 10,181
  • 7
  • 62
  • 98
  • 3
    I wonder how can we stop the flood of these questions. But even logically thinking, why anyone needs to match a whole string more than one time? Why add `g` modifier at all when you use `^` and `$` anchors? – Wiktor Stribiżew May 11 '17 at 11:12
  • Thanks for the close @WiktorStribiżew, it has solved my problem. I did attempt to search but did not find the question about the global flag. – James Monger May 11 '17 at 11:13

0 Answers0