JavaScript Regular Expressions
- Previous Page JS Bitwise Operations
- Next Page JS Operator Precedence
Regular expressions are the character sequence that constitutes
This search pattern can be used for text search and text replacement operations.
What is a regular expression?
Regular expressions are the constituentSearch pattern (search pattern)character sequence.
When you search for data in text, you can use search patterns to describe what you are searching for.
Regular expressions can be a single character, or a more complex pattern.
Regular expressions can be used to perform all types ofText searchandText replacementoperation.
syntax
/pattern/modifiers;
Example
var patt = /codew3c/i;
Example explanation:
/codew3c/i
is a regular expression.
codew3c
is the pattern (pattern) used in the search).
i
are modifiers (change the search to case-insensitive).
Use the string method
In JavaScript, regular expressions are commonly used in twoString methods:search()
and replace()
.
search()
The method uses the expression to search for matches and then returns the position of the match.
replace()
The method returns the modified string where the pattern was replaced.
Use the string method search() to process the string
search()
The method also accepts a string as the search parameter. The string parameter will be converted to a regular expression:
Example
Use a string to perform a search for "W3school":
var str = "Visit CodeW3C.com!"; var n = str.search("W3School");
Use regular expressions in the string method search()
Example
Perform a case-insensitive search for the string "codew3c" within the search string using regular expressions:
var str = "Visit CodeW3C.com"; var n = str.search(/codew3c/i);
The result of n will be:
6
Use the string method replace() to process strings
replace()
Also accepts strings as search parameters:
var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "W3School");
Use regular expressions in the string method replace()
Example
Use case-insensitive regular expressions to replace 'Microsoft' with 'W3school' in the string:
var str = "Visit Microsoft!"; var res = str.replace(/microsoft/i, "W3School");
The result of res will be:
Visit CodeW3C.com!
Did you notice?
Regular expression parameters (instead of string parameters) can be used in the above methods.
Regular expressions can make your search more powerful (for example, case-insensitive).
Regular expression modifier
ModifierCan be used for case-insensitive search at a more global level:
Modifier | Description | Try it |
---|---|---|
i | Perform case-insensitive matching. | Try it |
g | Perform global matching (find all matches instead of stopping after finding the first match). | Try it |
m | Perform multiline matching. | Try it |
Regular expression pattern
ParenthesesUsed to find strings within a certain range:
Expression | Description | Try it |
---|---|---|
[abc] | Find any character within square brackets. | Try it |
[0-9] | Find any digit from 0 to 9. | Try it |
(x|y) | Find any of the options separated by '|'. | Try it |
Metacharacter (Metacharacter)They are characters with special meanings:
Metacharacter | Description | Try it |
---|---|---|
\d | Find digits. | Try it |
\s | Find whitespace characters. | Try it |
\b | Match word boundaries. | Try it |
\uxxxx | Find Unicode characters specified by the hexadecimal number xxxx. | Try it |
Quantifiers Define quantifiers:
Quantifier | Description | Try it |
---|---|---|
n+ | Match any string containing at least one 'n'. | Try it |
n* | Match any string containing zero or more 'n's. | Try it |
n? | Match any string containing zero or one 'n'. | Try it |
Use the RegExp object
In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.
Use test()
test()
It is a regular expression method.
It searches for strings through patterns and then returns true or false based on the results.
The following example searches for the character 'e' in the string:
Example
var patt = /e/; patt.test("The best things in life are free!");
Since there is an 'e' in the string, the output of the above code will be:
true
You do not need to put the regular expression into a variable first. The two lines above can be shortened to one line:
/e/.test("The best things in life are free!");
Using exec()
exec()
A method is a regular expression method.
It searches the string through the specified pattern and returns the text found.
If no match is found, it returns null.
The following example searches for the character 'e' in the string:
Example
/e/.exec("The best things in life are free!");
Since there is an 'e' in the string, the output of the above code will be:
e
Complete RegExp Reference Manual
For a complete reference manual, please visit our full JavaScript RegExp Reference Manual.
This reference manual includes descriptions and examples of all RegExp properties and methods.
- Previous Page JS Bitwise Operations
- Next Page JS Operator Precedence