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

String.prototype.replaceAll() β€” regex with g flag

πŸ“– 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() β€” Regex with /g

When passing a regex to replaceAll, it must have the g flag.

Challenge: Implement removeVowels(str) that removes all vowels using a regex.

removeVowels('hello world') // β†’ 'hll wrld'
πŸ’‘ Hints (2)
  • β€’ When using a regex with `replaceAll`, the `/g` flag is required.
  • β€’ `/[aeiou]/gi` matches all vowels case-insensitively.