JavaScript regular expressions methods

In the following table, you can find the methods used to match or test a regular expression. The main JavaScript objects used in regular expressions are String and RegExp, which represent a pattern (such as regular expression).

Method

Description

Example

String.match(regular expression)

This executes a search for a match within a string, based on a regular expression.

var myString = "today is 12-12-2000";

var matches = myString.match(/\d{4}/);

//returns array ["2000"]

RegExp.exec(string)

This executes a search for a match in its string parameter. Unlike String.match, the parameter entered should be a string, not a regular expression pattern.

var pattern = /\d{4}/;

pattern.exec("today ...

Get JavaScript Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.