JS Built-ins
← String114 / 195
StringString.prototype.replaceAllintermediate

String.prototype.replaceAll() β€” empty string replacement (split-like)

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

Replacing the empty string '' inserts the replacement between every character (like splitting).

Challenge: Implement joinCharsWithDash(str) that inserts a dash between every character.

joinCharsWithDash('abc') // β†’ '-a-b-c-'
πŸ’‘ Hints (1)
  • β€’ Replacing `""` inserts the replacement at every position, including the start and end.