Hello Friends 👋,
Welcome To Infinitbility! ❤️
This tutorial will help you to replace the same strings or words from your string data. we will see two methods to replace all occurrences in a string.
Now we are going to replace using the below methods
replaceAll() method provides for replacing the same strings in your string data. but it’s available only in new updated modern browsers.
it also does not work in older node.js versions.
1const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';23console.log(p.replaceAll('dog', 'monkey'));
In the above example, we are replacing all occurrences of dog to monkey check below output.
replace() method plus regex global method we can also replace all occurrences of the same string.
it’s older way i.e it supports all places.
1const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';23console.log(p.replace(/dog/g, 'monkey'));
In the above code, we are replacing dog globally from string with monkey check below output.
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.