JavaScript Regular Expressions
Open W3Schools’s JavaScript RegExp Reference and regex101.com in separate browser windows. Select “ECMAScript (JavaScript)” as the regular expression flavor (you may need to expand the regex101.com window to see this option).
Write a regular expression to describe a phone number of the form
N-XXX-XXX-XXXX
where N
can only be a zero or a one. Look up the meaning of
^
and $
, and enclose your regular expression in parentheses to delineate a
“match group.” Do not permit 1-456-345-5678xxx
to be accepted.
Note: When entering a regular expression in JavaScript, you must always double the backslashes. For example, consider how to match three digits:
var pattern = new RegExp("(\\w\\w\\w)", "");
pattern.test(value);
is correct whereas
var pattern = new RegExp("(\w\w\w)", "");
pattern.test(value);
is invalid.