JavaScript RegExp
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 handling 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.
The /g Flag (Global)
The /g flag matches all occurrences of the pattern, rather than just the first one.
Example
A global search for "is" in a string:
let text = "Is this all there is?";
const pattern = /is/g;
let result = text.match(pattern);
The /i Flag (Insensitive)
The /i flag makes a match case-insensitive: /abc/i matches "abc", "AbC", "ABC".
Example
A case-insensitive search for "w3schools" in a string:
let text = "Visit W3Schools";
const pattern = /w3schools/i;
let result = text.match(pattern);
Learn More:
JavaScript RegExp Character Classes
Example [0-9]
A global search for the characters "0" to "9" in a string:
let text = "More than 1000 times";
const pattern = /[0-9]/g;
let result = text.match(pattern);
Learn More:
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 |
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?