JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Regular Expressions

Regular expressions, commonly known as "regex" or "RegExp", are patterns used to match character combinations in strings.

Regular expressions are one of the most powerful tools available today for effective and efficient text processing and manipulations.

Regular expressions can be used to perform all types of text search and text replace operations.

A regular expression can be a single character, or a more complicated pattern.

In JavaScript, regular expressions are also objects.


Creating a Regular Expression

You construct a regular expression in one of two ways:

Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:

Or calling the constructor function of the RegExp object, as follows:

Example explained:

  • parrot is a pattern (to be used in a search)
  • g is a modifier (performs a global match)

Using the constructor function provides runtime compilation of the regular expression.

Use the constructor function when you don't know the pattern and are getting it from another source, such as user input.

Note: The literal syntax uses forward slashes (/pattern/) to wrap the regular expression pattern, whereas the constructor syntax uses quotes ("pattern").


Using String Methods

In JavaScript, regular expressions are often used with the three string methods: search(), replace() and match().

The search() method uses an expression to search for a match, and returns the position of the match.

The replace() method returns a modified string where the pattern is replaced.

The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.


The search() Method

The search() method executes a search for a match between a regular expression and the string.

If the match is found it will return the position of the first match, and if the match is not found it will return -1:

Run code

The following example demonstrates the use of a regular expression with an i flag (ignore case):

Run code

Regular expressions can make your search much more powerful (case insensitive for example).


The replace() Method

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

The first parameter will be the value to be found, and the second parameter will be the value to replace it with.

Run code

By default, the replace() method replaces only the first match.

To replace all occurrences, use a regular expression with a g flag (global search):

Run code

To replace case insensitive, use a regular expression with an i flag (ignore case):

Run code

The match() Method

The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.

Run code

Regular Expression Modifiers

Modifiers are used to perform global searches, case sensitivity and searching in multiple lines:

Modifier Description Example
g Performs a global match that is, find all matches rather than stopping after the first match Demo
i Perform case-insensitive matching Demo
m Perform multiline matching Demo

Writing a Regular Expression Pattern

Regular expression patterns include the use of letters, digits, punctuation marks, etc., plus a set of special regular expression characters.

Brackets are used to find a range of characters:

Expression Description Example
[abc] Find any character between the brackets Demo
[0-9] Find any character between the brackets (any digit) Demo
(x|y) Find any of the alternatives specified Demo

A metacharacter is simply an alphabetical character preceded by a backslash that acts to give the combination a special meaning:

Metacharacter Description Example
\d Find a digit Demo
\W Find a non-word character Demo
\s Find a whitespace character Demo

The Quantifiers specifies frequency or position of bracketed character sequences:

Quantifier Description Example
z+ Matches any string that contains at least one z Demo
z* Matches any string that contains zero or more occurrences of z Demo
z? Matches any string that contains zero or one occurrences of z Demo

Using RegExp Methods

In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.

The test() method searches a string for a pattern, and returns true or false, depending on the result.

The exec() method searches a string for a pattern, and returns the found text as an object.


The test() method

The test() method executes a search for a match in a specified string.

If it finds a match, it returns true; otherwise, it returns false.

Use test() whenever you want to know whether a pattern is found in a string.

Run code

The test() returns a boolean, unlike the exec() which returns a string.


The exec() method

The exec() method executes a search for a match in a specified string.

This method returns the matched text if it finds a match; otherwise, it returns null.

Run code

Using Regex to verify input

Counting number of vowels in a string:

Enter some text in the input field to display the number of vowels:

VOWELS:

Run code

Complete RegExp Reference

For a complete properties and methods reference, visit our JavaScript RegExp Reference.

The reference section contains descriptions and examples of all RegExp properties and methods.