JS Built-ins
← String107 / 195
StringString.prototype.replaceintermediate

String.prototype.replace() β€” regex pattern

πŸ“– Usage Example
'foo bar foo'.replace('foo', 'baz')    // β†’ 'baz bar foo'
'hello'.replace(/[aeiou]/g, '*')       // β†’ 'h*ll*'

Use replace() to replace the first occurrence of a pattern; use a /g regex to replace all occurrences.

String.prototype.replace() β€” Regex Pattern

Pass a regex as the first argument to match by pattern instead of literal string.

Challenge: Implement removeDigits(str) that removes all digits from a string using a regex with the g flag.

removeDigits('a1b2c3') // β†’ 'abc'
removeDigits('hello')  // β†’ 'hello'
πŸ’‘ Hints (2)
  • β€’ `/\d/g` matches all digit characters (0–9) globally.
  • β€’ Replace with `""` to delete them.