O'Reilly logo

JavaScript Regular Expressions by Gabriel Manricks, Loiane Groner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required