I'm trying to make a regex which can match a phone number and so tell me if the phone number is either valid or not.
I would like to verify if there is a country id before the phone number, this one is well formated.
For example : +(33)0123456789 I want to be sure if the user start to type the first parenthesis it must be followed by number and ended by a closing parenthesis.
I have succeeded with PCRE engine
^[+]?((\()?(?(2)[0-9]{1,4})(?(2)\)))?([0-9]{2}){1}([\.\- ]?[0-9]{2}){4}$
But I realized this way doesn't work with javascript engine, conditional is not supported.
^[+]?((\()?((?=\2)[0-9]{1,4})((?=\2)\)))?([0-9]{2}){1}([\.\- ]?[0-9]{2}){4}$
It doesn't fill my needs. I want to check if the first parenthesis is set then it must be followed by number and a closing parenthesis.
So I ask you if there is a workaround in javascript to do this ?
Some help would be really appreciated, thank you :)
(\d{1,4})optional. Try^\+?((?:\([0-9]{1,4})\))?([0-9]{2})([. -]?[0-9]{2}){4}$, see this demo.