Javascript - .replace() and .replaceAll()
Code Life
By Pierre Marsaa
Swapping Part of a Text
Imagine you have a battle message template like "NAME uses Thunder!" and want to replace "NAME" with the actual Codémon's name.
The .replace(search, replacement) method searches a string and swaps a piece of text for another:
1.
The first argument is the text you want to find.
2.
The second argument is the new text that replaces it.
↗️
Loading editor...
↶
↷
Just like .slice(), .replace() returns a new string without modifying the original. This makes it easy to reuse the same template for different Codémon:
↗️
Loading editor...
↶
↷
Verify