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 |
---|---|
regexp | A regular expression or RegExp object.
|
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));
Related Tutorials
JavaScript Basic Tutorials - Lesson 8 - Strings