English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Regular expressions are usually called “regex” or “RegExp”, and are patterns used to match character combinations in strings.
Regular expressions are one of the most powerful tools available today, capable of effectively handling and manipulating text.
Regular expressions can be used to perform all types ofText searchandText replacementoperation.
Regular expressions can be a single character or a more complex pattern.
In JavaScript, regular expressions are also objects.
You can create a regular expression in one of the following two ways:
Use regular expressionstext,consisting of the pattern enclosed between slashes, as shown below:
var regex = /w3codebox/g
Or call the RegExp object'sConstructfunction, as shown below:
var regex = new RegExp("w3codebox", "g");
Example explanation:
w3codeboxis a pattern (for searching)
gis a modifier (for global matching)
UseConstructfunction can provide runtime compilation of regular expressions.
When you do not know the pattern and obtain the pattern from other sources (such as user input), please useConstructfunction.
Note:ThistextSyntax uses the forward slash (/pattern/)to enclose the regular expression pattern, while the constructor syntax uses quotes (“pattern”).
In JavaScript, the three string methods commonly used with regular expressions are:search(),replace()andmatch().
search()The method uses the expression to search for matches and then returns the position of the matches.
replace()The method returns a modified string in which the pattern will be replaced.
match()The method searches for matches with the regular expression in the string and then returns the matches as an Array object.
search()The method performs a search to find matches between the regular expression and the string.
If a match is found, it will return the position of the first match; if no match is found, it will return-1:
var str = 'The question is to be, or not to be, that is to be.'; var pos = str.search('to be';Test and see‹/›
The following examples demonstrate the use ofi
The use of modifiers in regular expressions (ignore case):
var str = 'The question is TO BE, or not to be, that is to be.'; var pos = str.search(/to be/i);Test and see‹/›
Regular expressions can make your search functionality more powerful (for example, case-insensitive).
replace()The method returns a new string that contains part or all of the pattern matches and is replaced by the replacement item.
The first parameter will be the value to be found, and the second parameter will be the value to be replaced.
var str1 = 'The question is to be, or not to be, that is to be.'; var str2 = str1.replace('to be', 'ZZZ';Test and see‹/›
By default,replace()The method only replaces the first match.
To replace all occurrences, use the modifier withg
Regular expression with modifier (global search):
var str1 = 'The question is to be, or not to be, that is to be.'; var str2 = str1.replace(/to be/g, 'ZZZ';Test and see‹/›
To replace case-insensitive code, use the modifier withi
Regular expression with modifier (ignore case):
var str1 = 'The question is TO BE, or not to be, that is to be.'; var str2 = str1.replace(/to be/gi, 'ZZZ';Test and see‹/›
match()The method searches for matches with the regular expression in the string and then returns the matches as an Array object.
var str = 'POLLUTION: Air Pollution is the introduction of chemicals to the atmosphere'; var reg = str.match(/ion/g);Test and see‹/›
Modifiers are used to perform global searches, case-sensitive, and multi-line searches:
Modifier | Description | Example |
---|---|---|
g | Execute global matching, that is, find all matches instead of stopping after the first match | Test it |
i | Execute case-insensitive matching | Test it |
m | Execute multi-line matching | Test it |
Regular expression patterns include the use of letters, numbers, punctuation marks, etc., as well as a set of special regular expression characters.
Square brackets []Used to find a series of characters:
Expression | Description | Example |
---|---|---|
[abc] | Find any character within the square brackets | Test it |
[0-9] | Find any character within the parentheses (any number) | Test it |
(x|y) | Find any specified replacement | Test it |
Meta-characterIs a simple letter character, preceded by a backslash, which gives this combination a special meaning:
Meta-character | Description | Example |
---|---|---|
\d | Find a digit | Test it |
\W | Find non-word characters | Test it |
\s | Find space characters | Test it |
QuantifierSpecify the position of the character sequence within the square brackets:
Expression | Description | Example |
---|---|---|
z+ | Matching any string containing at least onez'sString | Test it |
z* | Matching any string containing zero or morez'sString | Test it |
z? | Matching any string containing zero or one occurrencez'sString | Test it |
In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.
test()This method searches for the pattern in the string and returns true or false based on the result.
exec()This method searches for the pattern in the string and returns the found text as an object.
test()This method performs a search for the match in the specified string.
If a match is found, it returnstrue; otherwise, returnfalse.
Usetest()you want to know if the pattern you are looking for is found in the string.
var str = "www.oldtoolbag.com"; var regex = new RegExp("h"); var ans = regex.test(str);Test and see‹/›
Whentest()It returns a boolean value, which is different fromexec()It returns a string.
exec()This method performs a search for the match in the specified string.
If a match is found, this method returns the matched text; otherwise, it returns null.
var str = "www.oldtoolbag.com"; var regex = new RegExp("h"); var ans = regex.exec(str);Test and see‹/›
Calculate the number of vowels in the string:
Enter some text in the input field to display the number of vowel letters:
Enter the number of vowel letters:
For a complete reference of properties and methods, please visit ourJavaScript RegExp Reference.
The reference section includes descriptions and examples of all RegExp properties and methods.