Sunday 29 September 2019

javascript - Validation for xxx-xxx-xxxx or (xxx)xxx-xxxx










Whats the regex for




xxx-xxx-xxxx
or
(xxx)xxx-xxxx


I can create regex for the first one with



/^\d{3}-\d{3}-\d{4}$/



but how to add rule in this so it can also handle the second one?


Answer



You can use this: -



/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...