Regular Expressions
A Regular Expression is a sequence of characters that forms a search pattern.
Reg and Regex is a common shorthand for a regular expression.
JavaScript RexExp is an Object for storing Regular Expressions.
RegExp are be used for:
- Text searching
- Text replacing
- Text validation
Example
Do a case-insensitive search for "w3schools" in a string:
let text = "Visit W3Schools";
let n = text.search(/w3schools/i);
Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
Regular Expression Syntax
/pattern/modifier flags;
Using String Methods
Regular expressions are often used with the string methods:
Method | Description |
---|---|
match(regex) | Returns an Array of results |
replace(regex) | Returns a new String |
search(regex) | Returns the index of the first match |
Using String match()
Search for "W3schools" in a string:
let text = "Visit W3Schools";
let n = text.match(/W3schools/);
Using String replace()
Replace Microsoft with W3Schools in a string:
let text = "Visit Microsoft!";
let result = text.replace(/Microsoft/i, "W3Schools");
Try it Yourself »
Using String search()
Search for "W3Schools" in a string:
let text = "Visit W3Schools";
let n = text.search(/W3Schools/);
RexExp Alteration (OR)
In a regular expression an alteration is denoted with a vertical line character |.
An alteration matches any of the alternatives separated with |.
Example
A global search for the alternatives (red|green|blue):
let text = "Black, white, red, green, blue, yellow.";
let result = text.match(/red|green|blue/g);
RegExp Modifier Flags
Regular expression (regex) flags are optional parameters that can modify how a regex pattern is interpreted and applied.
They can change the behavior of a regex, such as making it case-insensitive or global (matching all occurrences), or enabling multiline seaches.
The /g Flag
Example
A global search for "is" in a string:
let text = "Is this all there is?";
let result = text.match(/is/g);
The /i Flag
Example
A case-insensitive search for "w3schools" in a string:
let text = "Visit W3Schools";
let result = text.match(/w3schools/i);
The /m Flag
Example
A multiline search for "is" at the beginning of each line in a string:
let text = "\nIs th\nis it?";
let result = text.match(/^is/m);
Full Flag Reference
Revised July 2025
Flag | Description |
---|---|
/d | Performs substring matches (new 2022) |
/g | Performs a global match (find all) |
/i | Performs case-insensitive matching |
/m | Performs multiline matching |
/s | Allows . (dot) to match line terminators (new 2018) |
/u | Enables Unicode support (new 2015) |
/v | An upgrade to the /u flag for better Unicode support (new 2025) |
/y | Performs a "sticky" search (new 2015) |
Regular Expression Methods
Regular Expression Search and Replace can be done with different methods.
These are the most common:
String Methods
Method | Description |
---|---|
match(regex) | Returns an Array of results |
matchAll(regex) | Returns an Iterator of results |
replace(regex) | Returns a new String |
replaceAll(regex) | Returns a new String |
search(regex) | Returns the index of the first match |
split(regex) | Returns an Array of results |
RegExp Methods
Method | Description |
---|---|
regex.exec() | Returns an Iterator of results |
regex.test() | Returns true or false |
See Also:
Exercise?What is this?
Test your skills by answering a few questions about the topics of this page
What does the i
modifier represent in Regular Expressions?