AppDividend
Latest Code Tutorials

Javascript String Replace Example Tutorial

925

Get real time updates directly on you device, subscribe now.

Javascript String Replace Example Tutorial is today’s leading topic. The replace() method searches the string for a specified value or keyword, or a regular expression, and returns the new string where the specified values are replaced. The pattern can be a String or the RegExp, and the replacement can be a string or a function to be called for each match.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript String Replace Example Tutorial

The syntax for the function replace() is the following.

str.replace(regexp|substr, newSubstr|function)

Now let us take an example.

let myStr = 'Bill is the richest person alive';

console.log(myStr.replace('Bill', 'Jeff'));

The replace() method does not change the String object itself it is called upon. It just returns a new string.

RegExp Match

Matching an exact substring is good, but often we find ourselves needing to match a pattern instead. It can quickly be done by using a regular expression as the first argument.

// server.js

let myStr = 'we can do whatever we want, we are venom';

myStr = myStr.replace(/w/g, 'b');

console.log(myStr);

You can check it via the node server. Just go to the terminal and go to the root of the above server.js file and type the following command.

node server

How to replace multiple occurrences of a substring in JavaScript

If we do not want to use the RegExp, then we can use the following technique to replace the substring with the multiple occurrences.

// server.js

let myStr = 'we can do whatever we want, we are venom';

String.prototype.replaceAll = function(search, replacement) {
  var target = this;
  return target.split(search).join(replacement);
};

console.log(myStr.replaceAll('w', 'b'));

So that is how we can replace the multiple occurrences.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Join the program

Replace Array of Strings

Here is an example where we can iterate over an array of strings and replace the substrings.

let ingredients = ['Y-MEN', 'Avengers', 'Loki'];

ingredients = ingredients.map(ingredient => {
  return ingredient.replace('Avengers', 'Revengers');
});

console.log(ingredients); //[ 'Y-MEN', 'Revengers', 'Loki' ]

Specifying a function as a parameter

We can define the function as a second parameter. In this case, the function will be invoked after the match has been performed. The function returns a result, and that result will be used as the replacement string. On the replace() function, we can also pass the second argument as a function and not the plain string. Let us see how we can use that.

let myStr = '13 million possibilities has been seen by Doctor Strange';

myStr = myStr.replace(/\d+/, (match) => {
  return +match + 1;
});

console.log(myStr);

 

Javascript String Replace Example Tutorial

Finally, Javascript String Replace Example Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.