JavaScript Regular Expressions
A regular expression is a set of characters that creates a pattern for searching.
You can use the search pattern to find and change text.
What Is a Regular Expression?
A regular expression is a set of characters that creates a search pattern.
When you look for information in a text, you can use a search pattern to specify what you're looking for.
A regular expression could be just one letter or a complex pattern.
Regular expressions are useful for searching and replacing text in various ways.
Syntax
Example
/Propertutorials/i;
Example explained:
The regular expression /ProperTutorials/i is used in JavaScript.
ProperTutorials is a term designed for searching.
i is a modifier that changes the search to be case-insensitive.
Using String Methods
In JavaScript, people commonly use regular expressions with two string methods: search()
and replace()
.
The search()
method finds a match using an expression and tells you where the match is located.
The replace()
function gives back a changed string with the new pattern instead of the old one.
Using String search() With a String
The search()
method looks for a specific value in a string and tells you where it finds it.
Using String search() With a Regular Expression
Using String replace() With a String
The replace()
method in JavaScript swaps one given value with another in a string.
Use String replace() With a Regular Expression
Did You Notice?
You can use regular expression arguments instead of string arguments in the methods mentioned above.
Regular expressions can boost your search by making it stronger, even if you don't worry about upper and lower case letters.
Regular Expression Modifiers
Modifiers help in conducting case-insensitive and broader 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 multiline matching |
Regular Expression Patterns
Brackets are used to find a range of characters:
Expression | Description |
---|---|
[abc] | Find any of the characters between the brackets |
[0-9] | Find any of the digits between the brackets |
(x|y) | Find any of the alternatives separated with | |
Characters that have a special meaning are called metacharacters.
Metacharacter | Description |
---|---|
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
Quantifiers express amounts or quantities:
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
Using the RegExp Object
JavaScript uses something called the RegExp
object, which is a set of rules and actions for working with patterns in text. This object has built-in features and ways of doing things.
Using test()
The test()
method is a part of regular expressions in JavaScript.
It looks for a specific pattern in a string and gives back either a true or false answer based on what it finds.
The following example searches a string for the character "e":
You don't have to put the regular expression in a variable first. The two lines above can be shortened to one:
/e/.test("The best things in life are free!");
Using exec()
The exec()
method is a RegExp expression method.
JavaScript looks through a set of characters for a particular arrangement and provides the located text as an object.
If there's no match, it gives back an empty object.
This example looks for the letter "e" in a string: