Skip to main content
InterviewHubPRO
Section Overview
Q352

Question 352 of 536

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

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

How do you return all matching strings against a regular expression

senior LevelCode ExampleAsync & PromisesEvent LoopHTTP & REST APIs

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