JS Built-ins
StringString.prototype.matchintermediate

String.prototype.match() β€” global flag returns all matches

πŸ“– Usage Example
'test 123 foo'.match(/d+/)    // β†’ ['123', index: 5, ...]
'test 1 and 2'.match(/d+/g)  // β†’ ['1', '2']

Use match() to search a string against a regex and return the match results, or null if not found.

String.prototype.match() β€” Global Flag

With the g flag, .match() returns an array of all matched strings (no capture group details).

Challenge: Implement allWords(str) that returns all word tokens using /\w+/g.

allWords('hello world foo') // β†’ ['hello', 'world', 'foo']
πŸ’‘ Hints (2)
  • β€’ With the `g` flag, `.match()` returns a flat array of all matched strings.
  • β€’ Use `?? []` to handle the null case when no matches are found.