JS Built-ins
StringString.prototype.matchintermediate

String.prototype.match() β€” basic regex match

πŸ“– 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()

str.match(regexp) returns an array with the match information when found, or null when not found.

Without the g flag, it returns the first match plus capture groups.

Challenge: Implement matchDigits(str) that returns the first sequence of digits in str using /\d+/.

matchDigits('abc123def') // β†’ '123'
matchDigits('no digits') // β†’ null
πŸ’‘ Hints (2)
  • β€’ Without the `g` flag, `.match()` returns details about the first match.
  • β€’ Access the matched text with `result[0]`.