May 06, 2021 JavaScript
1. JavaScript regular expression
2. What is a regular expression?
6. The search() method uses regular expressions
7. The search() method uses strings
8.. The replace() method uses regular expressions
10.. The replace() method uses strings
12.. Regular expression modifiers
13.. Regular expression pattern
20.. Complete RegExp reference manual
Regular expressions (English: Regular Expression, often short for regex, regexp, or RE in code) use a single string to describe and match a series of string search patterns that conform to a synth rule.
Search mode can be used for text search and text replacement.
Regular expressions are search patterns formed by a sequence of characters.
When you search for data in text, you can use search patterns to describe what you want to query.
Regular expressions can be a simple character, or a more complex pattern.
Regular expressions can be used for all text search and text replacement operations.
Instance resolution:
/w3cschool/i is a regular expression.
w3cschool is a pattern (for retrieval).
i is a modifier (search is case insensive).
In JavaScript, regular expressions are typically used for two string methods: search() and replace."
The search() method is used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression, and to return the starting position of the substring.
The replace() method is used to replace some characters in a string with others, or to replace a substring that matches a regular expression.
Use regular expressions to search for "w3cschool" strings without case insensescation:
The output is:
The search method can use strings as arguments. String parameters are converted to regular expressions:
Retrieve the substring of "w3cschool" in the string:
Replace Microsoft in the string with w3cschool with a regular expression and case insensition:
The output is:
The replace() method treats the receive string as an argument:
Regular expression parameters can be used in the above method (alternative to string arguments).
Regular expressions make search more powerful, such as case insensibility in instances. |
Modifiers can be case insensive in a global search:
Modifier | Describe |
---|---|
Ⅰ | Performs a case-insensitive match. |
G | Perform a global match (find all matches instead of stopping when the first match is found). |
M | Perform a multi-line match. |
Square brackets are used to find characters in a range:
The expression | Describe |
---|---|
[abc] | Find any character between square brackets. |
[0-9] | Look for any numbers from 0 to 9. |
(x|y) | Look for anything | The option to separate. |
Meta characters are characters with special meanings:
Meta-characters | Describe |
---|---|
\d | Find the number. |
\s | Look for blank characters. |
\b | Match word boundaries. |
\uxxxx | Look for Unicode characters specified in hex xxxx. |
Quantifiers:
Quantifiers | Describe |
---|---|
n+ | Matches any string that contains at least one n. |
N | Matches any string that contains zero or more n. |
N? | Matches any string that contains zero or an n. |
In JavaScript, the RegExp object is a regular expression object that predefines properties and methods.
The test() method is a regular expression method.
The test() method is used to detect whether a string matches a pattern, and if the string contains matching text, true is returned, otherwise false is returned.
The following example is used to search for the character "e" in the string:
The string contains "e", so the instance outputs as:
You can combine the above two lines of code into one line without setting variables for regular expressions:
The exec() method is a regular expression method.
The exec() method is used to retrieve a match for a regular expression in a string.
The function returns an array that holds 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 the string:
The string contains "e", so the instance outputs as:
The compile() method is used to change RegExp.
Compile() can between changing the retrieval mode or adding or deleting a second parameter.
Because there is an "e" in the string and no "d", the output of the above code is:
For a complete RegExp object reference manual, please refer to our JavaScript RegExp reference manual.
This reference manual contains methods and properties for all RegExp objects.
If you want to know what regular expressions are commonly used by js, refer to the js playbook.
JavaScript regular expression online test tool