function is used. The
function takes two arguments, the substring to be replaced and the new string that will take its place. Regex(p) can also be used to replace text in a string.
Replacing text within a string is a common operation in any programming language. For example, you may want to remove certain symbols from a string to make sure your program will work.
The replace() method accepts two arguments:
- The pattern or regular expression for which replace() should search; and
- A string which should replace any instances of the pattern you specified in the first argument.
Let’s take a look at this syntax:
var name = "Pieter"; console.log(name.replace("Pi", "P"));
Our first argument, “Pi”, is the pattern whose contents we want to replace. The second argument, “P”, is the text we want to replace our pattern with. So, our code replaces “Pi” with “P”.
The replace() method is added to the end of our string. You should not specify the string from which you want to replace values. Only the pattern for which you want to search and the text which should replace any instances of that pattern are accepted arguments.
Let’s say, for example, you want to replace the word interesting with intriguing in a string. How would you do that? We could employ the use of the replace() method:
var ourInterestingString = "This string is interesting."; var ourIntriguingString = ourInterestingString.replace("interesting", "intriguing"); console.log(ourIntriguingString);
Our code returns a new string which is as follows:
"This string is intriguing."
We can see that the word “interesting” has been replaced with “intriguing” in our string.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
If we want to replace multiple parts of our string, we can do that by chaining our replace() functions. Chaining means that we place multiple replace() statements after one another. Here’s an example:
var ourInterestingString = "This string is interesting."; var ourNewString = ourInterestingString .replace("This", "Our") .replace("string", "code") .replace("interesting.", "amazing!"); console.log(ourNewString);
Our code returns the following:
"Our code is amazing!"
Our code replaces all occurrences of the words “ This” , “ string” , and “ interesting” , with the words “ Our” , “ code” , “ amazing!”, respectively. Then it substitutes three words with three others in our string using the replace() function.
Regular expression , or regex(p) , defines a search pattern. When you’re looking to find or replace characters within a string, using regex can be useful. Regexp objects give us more control over our replace() function, so we can do more exciting replacements.
Here’s an example of using regex to replace the letter e with the letter f :
var ourInterestingString = “This string is interesting.”; var ourNewString = ourInterestingString .replace(“This”, “Our”) .replace(“string”, “code”) .replace(“interesting.”, “amazing!); console.log(ourNewString);
The output for our code is as follows:
"This string is interesting."
Our code first declares the variable ourInterestingString , and assigns it the value This string is interesting . Then, our code replaces every e throughout the string with the letter f , and the function returns our new string.
We use the /e/g/ regex expression to do this. This pattern will replace every e that comes up throughout the whole string (which is what the g signifies).
If you’re looking to learn more about regex, check out Regexr , which allows you to easily try out different expressions.