JavaScript Regular Expressions
A regular expression (RegExp) is a sequence of characters that forms a search pattern. Regular expressions can be used for text search, text replace, and input validation.
Creating a Regular Expression
There are two ways to create a regular expression in JavaScript: using a literal notation with forward slashes, or using the RegExp constructor.
// Literal notation
const pattern1 = /hello/;
console.log(pattern1); // /hello/
// Constructor notation
const pattern2 = new RegExp('hello');
console.log(pattern2); // /hello/
// With flags
const pattern3 = /hello/gi;
const pattern4 = new RegExp('hello', 'gi');
console.log(pattern3); // /hello/gi
console.log(pattern4); // /hello/giThe test() Method
The test() method tests for a match in a string. It returns true if it finds a match, otherwise it returns false.
const pattern = /world/;
const str = 'Hello world!';
console.log(pattern.test(str)); // true
console.log(pattern.test('Hello!')); // false
// Case-insensitive test
const pattern2 = /WORLD/i;
console.log(pattern2.test(str)); // trueThe exec() Method
The exec() method executes a search for a match in a string. It returns an array of information about the match, or null if no match is found.
const pattern = /quick (brown)/;
const str = 'The quick brown fox';
const result = pattern.exec(str);
console.log(result[0]); // 'quick brown'
console.log(result[1]); // 'brown'
console.log(result.index); // 4
console.log(result.input); // 'The quick brown fox'
// No match returns null
const noMatch = /cat/.exec(str);
console.log(noMatch); // nullRegular Expression Flags
Flags change how the regular expression behaves. They can be added after the closing slash of a literal or as a second argument to the RegExp constructor.
| Flag | Description |
|---|---|
| g | Global search — find all matches, not just the first |
| i | Case-insensitive search |
| m | Multiline — ^ and $ match start/end of each line |
| s | Dotall — . matches newline characters too |
| u | Unicode — treat pattern as a sequence of Unicode code points |
| y | Sticky — matches only from the index indicated by lastIndex |
// Global flag - find all matches
const str = 'cat bat cat bat';
console.log(str.match(/cat/g)); // ['cat', 'cat']
// Case-insensitive flag
console.log(/hello/i.test('Hello World')); // true
// Multiline flag
const multiline = 'First line\nSecond line';
console.log(multiline.match(/^Second/m)); // ['Second']
console.log(multiline.match(/^Second/)); // null
// Dotall flag
console.log(/hello.world/s.test('hello\nworld')); // true
console.log(/hello.world/.test('hello\nworld')); // falseCommon Use Cases
Regular expressions are commonly used for validating input, searching and replacing text, and extracting information from strings.
// 1. Validate an email (basic)
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
console.log(emailPattern.test('user@example.com')); // true
console.log(emailPattern.test('invalid-email')); // false
// 2. Search and replace
const text = 'I love cats. Cats are great!';
const result = text.replace(/cats/gi, 'dogs');
console.log(result); // 'I love dogs. dogs are great!'
// 3. Extract numbers from a string
const data = 'Order 123 has 5 items at $49.99';
const numbers = data.match(/\d+\.?\d*/g);
console.log(numbers); // ['123', '5', '49.99']
// 4. Split by multiple delimiters
const csv = 'one,two;three four';
const parts = csv.split(/[,;\s]+/);
console.log(parts); // ['one', 'two', 'three', 'four']