EN
JavaScript - String match() method example
0
points
// ONLINE-RUNNER:browser;
const expression = /\s/g; // match whitespaces
const text = 'Example text.';
if (text.match(expression)) {
console.log('The string contains space(s).');
}
1. Documentation
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. |
2. More examples
In this example, we use the match()
method to return an array of digits from the text
string.
// ONLINE-RUNNER:browser;
const expression = /[0-9]/g; // match digits (0...9)
const text = 'Example number: 1234';
const digits = text.match(expression);
console.log(digits);
Output:
[ '1', '2', '3', '4' ]