String.prototype.replaceAllbeginner'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.
.replace(str) only replaces the first match. .replaceAll(str) replaces all matches.
Challenge: Implement countReplacements(str, from, to) and show it replaces more than replace alone would.
'aaa'.replace('a', 'b') // β 'baa'
'aaa'.replaceAll('a', 'b') // β 'bbb'