EN
JavaScript - String match() method example
0 points
xxxxxxxxxx
1
const expression = /\s/g; // match whitespaces
2
3
const text = 'Example text.';
4
5
if (text.match(expression)) {
6
console.log('The string contains space(s).');
7
}
Syntax | String match(regexp) |
Parameters | regexp - a regular expression object |
Result |
The method returns an array containing the results of that search, or |
Description | The match() method retrieves the result of matching a string against a regular expression. |
In this example, we use the match()
method to return an array of digits from the text
string.
xxxxxxxxxx
1
const expression = /[0-9]/g; // match digits (0...9)
2
3
const text = 'Example number: 1234';
4
const digits = text.match(expression);
5
6
console.log(digits);
Output:
xxxxxxxxxx
1
[ '1', '2', '3', '4' ]