String.match
match — Matches a string with a string or regular expression.
Description
String.match(mixed comparison)
Parameters
Name |
Description |
Type |
Default |
Optional |
comparison |
String or regexp |
mixed |
|
No |
Return values
mixed: array or null if not matched.
Examples
Example #1 – match example
There a multiple ways to pass in the expression.
console.log('Hello World'.match(RegExp('l'))); // ["l", index: 2, input: "Hello World"]
console.log('Hello World'.match(new RegExp('l'))); // ["l", index: 2, input: "Hello World"]
console.log('Hello World'.match(/l/)); // ["l", index: 2, input: "Hello World"]
console.log('Hello World'.match('l')); // ["l", index: 2, input: "Hello World"]
Example #2 – match example
No match.
console.log('Hello World'.match(/a/)); // null
Example #3 – match example
Matching groups.
var str = 'See page 10.';
var regexp = /page (\d+)/;
console.log(str.match(regexp)); // ["page 10", "10", index: 4, input: "See page 10."]
External references