JavaScript Regular Expressions
A regular expression (English: Regular Expression, often abbreviated as regex, regexp, or RE in code) uses a single string to describe and match a series of strings that conform to a certain syntactic rule.
Search patterns can be used for text search and text replacement.
What is a Regular Expression?
A regular expression is a sequence of characters that forms a search pattern.
When you search for data in a text, you can use this search pattern to describe what you are looking for.
A regular expression can be a single character, or a more complex pattern.
Regular expressions can be used in all text search and text replacement operations.
Syntax
/regular expression body/modifiers(optional)
Where modifiers are optional.
Example:
var patt = /tutorialpro/i
Example explanation:
/tutorialpro/i is a regular expression.
tutorialpro is the regular expression body (used for retrieval).
i is a modifier (case-insensitive search).
Using String Methods
In JavaScript, regular expressions are commonly used with the two string methods: search() and replace().
The search() method is used to retrieve a specified substring or a substring that matches a regular expression, and returns the starting position of the substring.
The replace() method is used to replace some characters with others in a string, or to replace a substring that matches a regular expression.
Using search() Method with Regular Expression
Example
Using a regular expression to search for the string "tutorialpro" in a case-insensitive manner:
var str = "Visit tutorialpro!";
var n = str.search(/tutorialpro/i);
Output result:
6
Using search() Method with String
The search method can use a string as a parameter. The string parameter will be converted to a regular expression:
Example
Retrieve the substring "tutorialpro" in the string:
var str = "Visit tutorialpro!";
var n = str.search("tutorialpro");
Using replace() Method with Regular Expression
Example
Using a regular expression and case-insensitivity to replace "Microsoft" with "tutorialpro" in the string:
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"tutorialpro");
Output result:
Visit tutorialpro!
Using replace() Method with String
The replace() method will receive a string as a parameter:
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","tutorialpro");
Did You Notice?
| | Regular expression parameters can be used in the above methods (instead of string parameters). <br>Regular expressions make search functions more powerful (as shown in the case-insensitive example). | | --- | --- |
Regular Expression Modifiers
Modifiers can be used for case-insensitive global searches:
Modifier | Description |
---|---|
i | Perform case-insensitive matching. |
g | Perform a global match (find all matches rather than stopping after the first match). |
m | Perform multi-line matching. |
Regular Expression Patterns
Brackets are used to find a range of characters:
Expression | Description | ||
---|---|---|---|
[abc] | Find any character between the brackets. | ||
[0-9] | Find any digit from 0 to 9. | ||
(x | y) | Find any of the alternatives separated by | . |
Metacharacters are characters with a special meaning:
Metacharacter | Description |
---|---|
\d | Find a digit. |
\s | Find a whitespace character. |
\b | Match a word boundary. |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx. |
Quantifiers:
Quantifier | Description |
---|---|
n+ | Match any string that contains at least one n. |
n* | Match any string that contains zero or more occurrences of n. |
n? | Match any string that contains zero or one occurrences of n. |
Using RegExp Object
In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.
Using test()
The test() method is a regular expression method.
The test() method is used to test whether a string matches a pattern. If the string contains matching text, it returns true; otherwise, it returns false.
The following example is used to search for the character "e" in a string:
Example
var patt = /e/;
patt.test("The best things in life are free!");
The string contains "e", so the example outputs:
true
You can combine the two lines of code into one:
/e/.test("The best things in life are free!")
Using exec()
The exec() method is a regular expression method.
The exec() method is used to retrieve the matches of a regular expression in a string.
The function returns an array containing the matching results. If no match is found, the return value is null.
The following example is used to search for the letter "e" in a string:
Example 1
/e/.exec("The best things in life are free!");
The string contains "e", so the example outputs:
e
More Examples
- JS Check if Input String is Composed of Numbers, Letters, and Underscores
- JS Check if Input String is Entirely Letters
- JS Check if Input String is Entirely Numbers
Complete RegExp Reference
For the complete reference manual of the RegExp object, please refer to our JavaScript RegExp Reference.
This reference manual includes all methods and properties of the RegExp object.