RegExp
Added in ves6 · Updated March 16, 2026 · Built-in Objects
javascript regexp es6 string
RegExp is a built-in object that represents regular expressions for pattern matching and text manipulation. Regular expressions are powerful tools for searching, replacing, and extracting text based on patterns.
Creating RegExp Objects
Create a RegExp using the constructor or a literal notation:
// Literal notation (preferred)
const regex1 = /pattern/;
const regex2 = /pattern/flags;
// Constructor
const regex3 = new RegExp("pattern");
const regex4 = new RegExp("pattern", "flags");
Flags
Flags modify how the regular expression behaves:
| Flag | Description |
|---|---|
g | Global — find all matches |
i | Case-insensitive matching |
m | Multiline — ^ and $ match line boundaries |
s | Dotall — . matches newlines |
u | Unicode — enable full Unicode support |
y | Sticky — match only from lastIndex |
const caseInsensitive = /hello/i;
const globalMatch = /o/g;
const multiline = /^line/m;
Basic Patterns
Character Classes
/\d/ // Matches any digit [0-9]
/\D/ // Matches any non-digit
/\w/ // Matches word character [a-zA-Z0-9_]
/\W/ // Matches non-word character
/\s/ // Matches whitespace
/\S/ // Matches non-whitespace
/[abc]/ // Matches any character in brackets
/[^abc]/ // Matches any character NOT in brackets
/[a-z]/ // Matches any character in range
Anchors
/^hello/ // Matches at start
/world$/ // Matches at end
/\bword/ // Matches at word boundary
/\Bword/ // Matches at non-word boundary
Quantifiers
/a*/ // Zero or more
/a+/ // One or more
/a?/ // Zero or one
/a{3}/ // Exactly 3
/a{2,}/ // 2 or more
/a{2,5}/ // Between 2 and 5
Special Characters
/\./ // Escaped dot (matches literal .)
/\n/ // Newline
/\t/ // Tab
/\r/ // Carriage return
/\\/ // Backslash
Methods
test()
Tests if a pattern exists in a string. Returns true or false:
const regex = /hello/;
regex.test("hello world"); // true
regex.test("goodbye"); // false
exec()
Executes a search and returns an array with match details or null:
const regex = /(\d+)-(\d+)-(\d+)/;
const result = regex.exec("2024-03-16");
result[0]; // "2024-03-16" (full match)
result[1]; // "2024" (first capture group)
result[2]; // "03" (second capture group)
result[3]; // "16" (third capture group)
result.index; // 0 (position of match)
String Methods
match()
Returns matches found in a string:
"hello world".match(/o/g);
// ["o", "o"] (with g flag)
"hello".match(/o/);
// ["o", index: 4, input: "hello"] (without g flag)
matchAll()
Returns an iterator of all matches with capture groups:
const regex = /(\d+)-(\d+)/g;
const matches = "12-34 56-78".matchAll(regex);
for (const match of matches) {
console.log(match[0], match[1], match[2]);
}
// "12-34" "12" "34"
// "56-78" "56" "78"
replace()
Replaces matched text:
"hello world".replace(/world/, "there");
// "hello there"
"cat cat cat".replace(/cat/g, "dog");
// "dog dog dog"
"John Smith".replace(/(\w+) (\w+)/, "$2, $1");
// "Smith, John"
split()
Splits string by pattern:
"a,b,c".split(/,/);
// ["a", "b", "c"]
"one1two2three".split(/\d/);
// ["one", "two", "three"]
search()
Returns index of first match or -1:
"hello world".search(/world/); // 6
"hello".search(/xyz/); // -1
Practical Examples
Email Validation
function isValidEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
isValidEmail("user@example.com"); // true
isValidEmail("invalid"); // false
Extracting Numbers
const text = "Price: $19.99";
const match = text.match(/\d+\.\d+/);
if (match) {
console.log(parseFloat(match[0])); // 19.99
}
Password Strength
function isStrongPassword(password) {
return /.{8,}/.test(password) && // At least 8 chars
/[A-Z]/.test(password) && // Uppercase
/[a-z]/.test(password) && // Lowercase
/[0-9]/.test(password); // Number
}
See Also
- String.prototype.match() — Retrieve matches from a string
- String.prototype.matchAll() — Iterator of all matches
- String.prototype.replace() — Replace matched text