Replace vs Replace All
22 days ago
1 min read

Replace vs Replace All

Hello Everyone, Welcome to my blog 🙏. I hope you are all safe and that this blog finds you in good health ❤️.

In this blog, we will learn about

Replace() vs ReplaceAll()

let description = "BOB is a builder."    

Replace “BOB” with “TONY”?    

The answer is very simple and straight forward we can use “replace”.    

Solution: description.replace(“BOB”, “TONY”). 

Now if there are multiple BOB strings inside the description Such as 

 let description = "BOB is a builder. BOB is very great. BOB can build 10 storey building in 10 minutes."    

In this situation, it is clearly stated ReplaceAll() would be helpful.    

Solution: description.replaceAll(“BOB”, “TONY”).

That’s great!!

But Is it possible to do it using “Replace()” alone?

The answer is YES!! How????

Using regex(Regular Expression)

description.replace(/BOB/g, “TONY”) 

If you miss “g”, then it will work as replace

description.replace("BOB", "TONY") === description.replace(/BOB/, "TONY")

--------------------------------------------------------------------------------------------------------

Here we go, That’s it folks for this blog.

Hope everyone liked it. If you like it, give it a love ❤️ , and share it with your friend.

For more exciting content on Frontend,

Please do follow me 🎯.

Thanks a lot, Everyone 🙏. Until Next time, Happy Learning ✍️

Abhishek Kovuri, UI developer

Appreciate the creator