Loading lesson path
Character Classes are characters enclosed in square brackets []. A character class matches any character from a set within brackets:
// Match Digits const pattern = /[0-9]/;[a]
[^a]
[abc]
[^abc]
Formula
[a - z]Matches all characters in the range from a to z
Formula
[^a - z]Matches all characters NOT in the range from a to z
Formula
[0 - 9]Matches all characters in the range from 0 to 9
Formula
[^0 - 9]Matches all characters NOT in the range from 0 to 9
Example [HW]
A global search for the characters "H" and "W" in a string:
let text = "Hello World!";
const pattern = /[HW]/g;
let result = text.match(pattern);Example [A-Z]
A global search for the upper case characters "A" to "Z" in a string:
let text = "This is W3School";
const pattern = /[A-Z]/g;
let result = text.match(pattern);Example [1234]
A global search for the characters "1", "2", "3" and "4" in a string:
let text = "123456789";
const pattern = /[1234]/g;
let result = text.match(pattern);Example [1-4]
A global search for the characters "1" to "4" in a string:
let text = "123456789";
const pattern = /[1-4]/g;
let result = text.match(pattern);Formula
[01234] is the same as [0 - 4]
[abcde] is the same as [a - e]Replace can be done with different methods.
regex )
regex )
regex )
regex )
regex ) Returns the index of the first match split( regex )
Description regex.exec() Returns an Iterator of results regex.test()