match()  String  instance method getterS2C Home  « Globals « String « match()

Description

Return match(es) of a string and regular expression comparison.

  • The pattern to replace can be a string or a regular expresion.
  • For each match the pattern can be replaced by another string or the result of a called function.

Syntax

Signature Description
aString.match(regexp)Return match(es) of a string and regular expression comparison.

Parameters

Parameter Description
regexpA regular expression or RegExp object.
  • The object passed is implicitly converted to a RegExp object if it isn't already.
  • Returns same result as the regular expression regular expression RegExp.exec() method when no g flag is specified in the regular expression.
  • If g flag is specified in the regular expression and matches are found returns an Array of the matches.
  • If g flag is specified in the regular expression and NO matches are found returns null.

Examples

The code below replaces part of a string.


// Create a string variable and a regular expression.
var matchSentence = 'I like number 7, number 11 and of course number 13';
regex = new RegExp('\\d+', 'g');

/*
 match string with regexps.
 /*
alert(matchSentence.match(/\d/));
alert(matchSentence.match(/\d{2}/));
alert(matchSentence.match(regex));

Press the button below to action the above code:


Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings

go to home page Homepage go to top of page Top