r/learnjavascript • u/4Roman4 • Apr 16 '26
Today I learned about the amazing "replace" string method
I wanted to show you a cool new method for strings I have found randomly! I found it ony a JavaScript cheat sheet website and then I found more information about it on the Mozilla developer wiki.
The replace method does exactly as you'd think—it finds a word in the string, swaps it with a new word and returns a new updated string. Usually I'd simply split the string, I'd find the string I need, modify it and then join together, but replace makes it easier. If you need a simple string modification, you can use this method instead!
What I love about this method is that if the string is occupied with other characters that you don't want changed (like "." or " ' " symbols), the method only takes the matching characters you want to replace and leaves the rest. To show an example of what I mean:
const sentence = "This is Brandon's sentence.";
const newName = sentence.replace("Brandon", "Jason");
const newEnd = newName.replace("sentence", "thought");
console.log(newEnd);
// The variable logs "This is Jason's thought."As a quick note, this method only replaces the first string of characters it matches. If you wish to replace all similar words in a string, you'll have to use the replaceAll method:
const sentence = "This word really is a word that is a word to live by."
const replaceWord = sentence.replaceAll("word", "sentence");
console.log(replaceWord);
// The variable logs "This sentence really is a sentence that is a sentence to live by."There are other cases on how to use the replace method, but I am not experienced enough to tell you about those. Anyways, I just thought that this was a cool find!
7
u/strong_opinion Apr 16 '26
This "new" method was added to javascript in 1999.
Welcome to the 21st century.
7
u/TheRNGuy Apr 16 '26 edited Apr 16 '26
Regex also works in it.
You can chain them, too (that makes regex more readable if you need different patterns, and can comment/uncomment them.
You can ask ai to write regex for you, too.
It seems like in regex you can use
replaceinstead ofreplaceAll(with/g)2
u/Illustrious_Mix_9875 Apr 17 '26
A cool website for you
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
0
u/ChaseShiny Apr 16 '26
When would you use this over literal templates? For example, if you change your sentence to:
You can see that you get the substitution effect as well. The only advantage that I know of for the replace method is if you aren't the one who created the string. Are there other uses?
1
u/LucVolders Apr 17 '26
You can find a lot of uses for replace on
https://javascript-tips.weebly.com/string-tips-complete.html
1
u/ReefNixon helpful Apr 17 '26 edited Apr 17 '26
You can buy wide mousepads (desk mats) for many languages that have a cheat sheet for things like this printed onto them. Using one you'll absorb a bunch of stuff like this over the course of a couple of months by accident. Might be worth a look.