replace() function is used. The
replace() 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.
replace() function and explore how it can be used to amend the text.
Strings are a sequence of one or more characters that may include letters, symbols, spaces, and numbers. Strings are an important data type in any programming language because they allow us to use text to communicate with a computer. A typical string will look like this:
var str = "I am an example string!";
Strings can also use single quotes (
‘’), double quotes (
“”), or backticks (
``) interchangeably—they all have the capacity to hold a string. The only thing that matters is that you open and close your string using the same character.
string replace() function. The
Let’s say, for example, you want to replace the word
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."
A lot is going on here, so let’s break it down. On the first line, we declare the variable
ourInterestingString, which contains the value
This string is interesting. On the next line, we replace the word
string replace() method.
On the final line, we print the variable
ourIntriguingString to the console, which returns the value
This string is intriguing.
Additionally, if we want to replace multiple parts of our string, we can do that by chaining our
replace() functions. 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
interesting, with the words
amazing! respectively. Then it substitutes three words with three others in our string using the
String Replace() RegEx
Regular expression, or
regex(p), is a type of character that 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
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, which means that we should replace every
e that comes up throughout the whole string (which is what the
If you’re looking to learn more about regex, check out Regexr, which allows you to easily try out different expressions.
replace() function allows us to replace one part of a string with another and return the newly modified string. The
Regex() function allows us to do the same thing but with greater control.
replace() function, and also discussed how regex could be used to perform more advanced replace functions on a string.