InterviewHubPRO
Section Overview
Q352

Question 352 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

68%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

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,

javascript
  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