JavaScript Code

JavaScript String match()


String match()

In JavaScript, String.match() method is used to find all the matching substring with a regular expression.

Syntax

str.match(regexp)
  • str is a string.
  • match is function name.
  • regexp is a regular expression. If string is provided for this parameter, then the string is converted to regular expression.

match() method returns an array of values that are matched in the string str for the matching regexp

Examples

1. Find all matchings in the string str with the regular expression /apple/ig in str. this regular expression matches for the string 'apple' globally with case -insenitive

let str = 'apple banana Apple mango';
let matchings = str.match(/apple/ig);
console.log(matchings);


Copyright @2022