Types of Regular Expressions (RegEx) in JavaScript
Literal Characters
Match exact characters. Example: /hello/ matches "hello".
Metacharacters
Characters with special meanings, like . to match any character except newline. Example: /h.llo/ matches "hello", "hallo", etc.
Character Classes
\d: Matches any digit (equivalent to [0-9]).
let regex = /\d/;
console.log(regex.test("123")); // true
console.log(regex.test("abc")); // false
\D: Matches any non-digit character (equivalent to [^0-9])
let regex = /\D/;
console.log(regex.test("123")); // false
console.log(regex.test("abc")); // true
\w: Matches any word character (alphanumeric + underscore, equivalent to [A-Za-z0-9_]).
let regex = /\w/;
console.log(regex.test("hello_world123")); // true
console.log(regex.test("!@#$")); // false
\W: Matches any non-word character (equivalent to [^A-Za-z0-9_])
let regex = /\W/;
console.log(regex.test("hello_world123")); // false
console.log(regex.test("!@#$")); // true
\s: Matches any whitespace character (spaces, tabs, line breaks)
let regex = /\s/;
console.log(regex.test("hello world")); // true
console.log(regex.test("helloworld")); // false
\S: Matches any non-whitespace character
let regex = /\S/;
console.log(regex.test("hello world")); // true
console.log(regex.test(" ")); // false
Quantifiers
Specify how many times a character or group should appear
Example: /a{2,}/ matches "aa", "aaa", etc
*: Matches 0 or more occurrences of the preceding character
let regex = /a*/;
console.log("aaa".match(regex)); // ["aaa"]
+: Matches 1 or more occurrences of the preceding character.
let regex = /a+/;
console.log("aaa".match(regex)); // ["aaa"]
?: Matches 0 or 1 occurrence of the preceding character.
let regex = /colou?r/;
console.log(regex.test("color")); // true
console.log(regex.test("colour")); // true
Anchors
Assert position in the string.
Example: /^hello/ matches "hello" at the start, /world$/ matches "world" at the end.
^: Matches the start of a string.
let regex = /^hello/;
console.log(regex.test("hello world")); // true
console.log(regex.test("world hello")); // false
$: Matches the end of a string.
let regex = /world$/;
console.log(regex.test("hello world")); // true
console.log(regex.test("world hello")); // false
Groups and Ranges
- Group parts of a pattern or specify a range of characters
Example:
/(abc)/matches "abc",/[a-z]/matches any lowercase letter.
let regex = /(abc)/;
console.log(regex.test("abc")); // true
console.log(regex.test("abcd")); // true
[a-z]: Matches any lowercase letter
let regex = /[a-z]/;
console.log(regex.test("abc")); // true
console.log(regex.test("ABC")); // false
[A-Z]: Matches any uppercase letter.
let regex = /[A-Z]/;
console.log(regex.test("abc")); // false
console.log(regex.test("ABC")); // true
-[0-9]: Matches any digit
let regex = /[0-9]/;
console.log(regex.test("123")); // true
console.log(regex.test("abc")); // false
Alternation
Use | to match one of several patterns.
Example: /cat|dog/ matches "cat" or "dog"
let regex = /cat|dog/;
console.log(regex.test("I have a cat.")); // true
console.log(regex.test("I have a dog.")); // true
console.log(regex.test("I have a bird.")); // false
Escaped Characters
Escape metacharacters to match them literally.
Example: /\./ matches the period character.
let regex = /\\./;
console.log(regex.test("hello.")); // true
console.log(regex.test("hello")); // false
Advanced Examples
Matching an Email Address
let regex = /^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$/;
console.log(regex.test("example@example.com")); // true
console.log(regex.test("example.com")); // false
Matching a URL
let regex = /^(https?:\\/\\/)?([\\da-z.-]+)\\.([a-z.]{2,6})([\\/\\w .-]*)*\\/?$/;
console.log(regex.test('https://www.example.com')); // true
console.log(regex.test('ftp://example.com')); // false
Extracting Numbers from a String
let regex = /\\d+/g;
console.log("The year is 2023.".match(regex)); // ["2023"]
Replacing Text
let regex = /cat/g;
console.log("The cat sat on the cat.".replace(regex, "dog")); // "The dog sat on the dog."
Validating a Phone Number
let regex = /^\\(\\d{3}\\) \\d{3}-\\d{4}$/;
console.log(regex.test("(123) 456-7890")); // true
console.log(regex.test("123-456-7890")); // false