Common Regex Patterns
Matching Email Addresses
A common use case for regular expressions is to validate email addresses. A typical email pattern requires a sequence of characters followed by an ”@” symbol, followed by another sequence of characters, a dot, and a domain.
const regexEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
// Examples:
console.log(regexEmail.test('test@example.com')); // true
console.log(regexEmail.test('user.name+tag+sorting@example.com')); // true
console.log(regexEmail.test('user@subdomain.example.com')); // true
console.log(regexEmail.test('invalid-email')); // false
Explanation:
^[a-zA-Z0-9._%+-]+
: Matches the user name part of the email, which can include letters, digits, dots, underscores, percent signs, plus signs, and hyphens.@
: Matches the ”@” symbol.[a-zA-Z0-9.-]+
: Matches the domain part of the email, which can include letters, digits, dots, and hyphens.\.[a-zA-Z]{2,}$
: Matches the top-level domain, which starts with a dot and includes at least two letters.
Matching URLs
Regex can also be used to validate URLs. A typical URL pattern requires a protocol (http, https), followed by ”://”, a domain name, and optionally a path.
const regexURL = /^(https?:\/\/)?([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})(\/[a-zA-Z0-9.-]*)*\/?$/;
// Examples:
console.log(regexURL.test('http://example.com')); // true
console.log(regexURL.test('https://www.example.com/path/to/page')); // true
console.log(regexURL.test('example.com')); // true
console.log(regexURL.test('ftp://invalid-url.com')); // false
Explanation:
^(https?:\/\/)?
: Matches the protocol part (http or https), which is optional.([a-zA-Z0-9.-]+)
: Matches the domain name, which can include letters, digits, dots, and hyphens.\.([a-zA-Z]{2,})
: Matches the top-level domain, which starts with a dot and includes at least two letters.(\/[a-zA-Z0-9.-]*)*\/?$
: Matches the path part, which can include slashes, letters, digits, dots, and hyphens. The trailing slash is optional.
Matching Phone Numbers
Phone numbers can vary widely in format, but a common pattern is to match digits, optionally grouped by spaces, dots, or hyphens.
const regexPhone = /^(\+?\d{1,3}[-.\s]?)?(\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d{4}$/;
// Examples:
console.log(regexPhone.test('+1-800-555-1234')); // true
console.log(regexPhone.test('(800) 555-1234')); // true
console.log(regexPhone.test('800.555.1234')); // true
console.log(regexPhone.test('5551234')); // true
Explanation:
^(\+?\d{1,3}[-.\s]?)?
: Matches the optional country code, which may start with a ”+” and be followed by 1 to 3 digits. It can be separated by a hyphen, dot, or space.(\(?\d{3}\)?[-.\s]?)?
: Matches the optional area code, which may be enclosed in parentheses and separated by a hyphen, dot, or space.\d{3}[-.\s]?\d{4}$
: Matches the local number part, which consists of 3 digits, followed by a hyphen, dot, or space, and then 4 digits.
Exercises
- Write a regular expression to match a valid IP address.
- Create a Regex pattern that matches a date in the format
YYYY-MM-DD
. - Write a regular expression to match hexadecimal color codes (e.g.,
#a3c113
).
Common Regex patterns are incredibly useful for validating and extracting specific types of data.