FrontendDeveloper.in

JavaScript question detail

How do you return all matching strings against a regular expression

The matchAll() method can be used to return an iterator of all results matching a string against a regular expression. For example, the below example returns an array of matching string results against a regular expression,

let regexp = /Hello(\d?)/g;
let greeting = "Hello1Hello2Hello3";

let greetingList = [...greeting.matchAll(regexp)];

console.log(greetingList[0][0]); //Hello1
console.log(greetingList[1][0]); //Hello2
console.log(greetingList[2][0]); //Hello3
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399