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

String.prototype.replaceAll() β€” no match returns original

πŸ“– 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() β€” No Match

If the search value is not found, replaceAll returns the original string unchanged.

Challenge: Implement tryReplaceAll(str, from, to) using replaceAll.

tryReplaceAll('hello', 'xyz', '!!!') // β†’ 'hello'
πŸ’‘ Hints (1)
  • β€’ `replaceAll` is case-sensitive by default.