WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Regular Expressions


Regular expression (English: Regular Expression, often abbreviated as regex, regexp or RE in the code) uses a single string to describe and match a series of string search patterns that meet a certain syntax rule.

Search mode is available for text search and text replacement.


What Is a Regular Expression?

A regular expression is a search pattern formed by a sequence of characters.

When you search for data in text, you can use search patterns to describe what you are looking for.

A regular expression can be a simple character or a more complex pattern.

Regular expressions can be used for all text search and text replacement operations.

Syntax

/ Regex body / modifier (optional)

Example

var patt =/welookups/i;

Example explained:

/welookups/i  is a standard expression.

welookups  is an example (to be utilized in a search).

i  is a modifier (changes the inquiry to be case-insensitive).


Using String Methods

Regular expressions are commonly used in JavaScript with two string methods: search () and replace ().

search () method is used to retrieve a specified substring in a string, or a substring that matches a regular expression, and returns the starting position of the substring.

replace () method is used to replace some characters in a string with another character, or to replace a substring that matches a regular expression.


search () method uses regular expressions

Example

Use regular expressions to search for "welookups" strings and is not case sensitive::

var str = "Visit welookups";
var n = str.search(/welookups/I);

The output is:

6
Try it Yourself »

Search () method uses strings

The search method can take a string as a parameter. String parameters are converted to regular expressions:

Example

Example

Use a string to complete a look for "welookups" in a string:

var str = "Visit welookups!";
var n = str.search("welookups");
Try it Yourself »

replace () method uses regular expressions

Example

Use regular expressions and case insensitivity to replace Microsoft in the string with welookups::

var str = "Visit Microsoft!";
var res = str.replace(/microsoft/I, "welookups");

The result in res will be:

Visit welookups!
Try it Yourself »

replace () method uses a string

The replace () method takes the received string as a parameter:

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "welookups");
Try it Yourself »

Did you notice?


Note Regular expression parameters can be used in the above method (instead of string parameters).
Regular expressions make search more powerful (eg case-insensitive in examples).


Regular expression modifier

Modifiers can be case-insensitive in global searches:

Modifier Description
Modifiers Description
i Perform case-insensitive matching.
g Perform global matches (find all matches instead of stopping after finding the first match).
m Perform multiple line matching.


Regular expression pattern

Square brackets are used to find a range of characters:

Expression Description
[abc] Find any characters between the square brackets.
[0-9] Find any number from 0 to 9.
(x | y) Find any options separated by |.

Metacharacters are characters with special meaning:

Metacharacters Description
\ d Find numbers.
\ s Find whitespace characters.
\ b Matches word boundaries.
\ uxxxx Find Unicode characters specified by the hexadecimal number xxxx.

Quantifier:

quantifiers Description
n + matches any string containing at least one n .
n * Matches any string containing zero or more n .
n? matches any string containing zero or one n .


Using RegExp objects

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.

test () method is used to detect whether a string matches a pattern. If the string contains matching text, it returns true, otherwise it returns false.

The following example searches 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 output is:

genuine
Try it yourself »

Instead of setting a regular expression variable, the above two lines of code can be combined into one line:

/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 regular expression matches in a string.

This function returns an array containing the matching results. If no match is found, the return value is null.

The following example searches 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 output is:

e
Try it yourself »