Languages
[Edit]
EN

JavaScript - String match() method example

0 points
Created by:
Hayley-Mooney
677
// 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

SyntaxString match(regexp)
Parametersregexp - a regular expression object
Result

The method returns an array containing the results of that search, or null if no matches are found.

DescriptionThe 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' ]

See also

  1. JavaScript - exec() vs match()

References

  1. String.prototype.match() - JavaScript | MDN

Alternative titles

  1. JavaScript - String.prototype.match() documentation with examples
  2. js - String match() method documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (documentation)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join