JS Built-ins
← String111 / 195
StringString.prototype.replaceAllbeginner

String.prototype.replaceAll() β€” replace all occurrences

πŸ“– Usage Example
'foo bar foo'.replaceAll('foo', 'baz')  // β†’ 'baz bar baz'
'banana'.replaceAll('a', 'o')           // β†’ 'bonono'

Use replaceAll() to replace every occurrence of a search string or regex (with /g flag) with the replacement.

String.prototype.replaceAll()

str.replaceAll(searchValue, replaceValue) replaces all occurrences of searchValue.

Challenge: Implement replaceAllOccurrences(str, from, to) that replaces every occurrence of from with to.

replaceAllOccurrences('foo bar foo', 'foo', 'baz') // β†’ 'baz bar baz'
πŸ’‘ Hints (1)
  • β€’ `replaceAll` is like `replace` with a `/g` flag regex but works with plain strings.