How to Replace String in JavaScript
Abstract: the replace() function will replace the first occurrence of the given string. let str = "Mr Bean has a green Apple and a Red Apple" let result = str.r
We can use replace() method to replace any string or character with another in javascript. It searches for a defined string, character, or regular expression and replaces it. This method doesn’t change the original string but it returns the updated string as result.
Syntax:
string.replace(old_string, new_string)Replace String in JavaScript with Examples
In this tutorial, we will discuss a few examples of replacing strings in Javascript.
Let’s try some examples:
- Here is the first example to initialize a text in a variable, then replace it with another text. let str = "Hello World!" let result = str.replace("World", "TecAdmin") console.log(result)1234let str = "Hello World!"let result = str.replace("World", "TecAdmin") console.log(result)
Run the above example and see the results. Thanks to playcode.io that help me to run javascript online.
Replace string in javascript - What happens if a given string is found multiple times. In that case, the
replace()
function will replace the first occurrence of the given string. let str = "Mr Bean has a green Apple and a Red Apple" let result = str.replace("Apple", "Strawberries") console.log(result)12345let str = "Mr Bean has a green Apple and a Red Apple" let result = str.replace("Apple", "Strawberries") console.log(result)Execute the above code and see the results:
Replace first matching string in javascriptThe result clearly shows that the first occurrence is replaced with a new string, but the second occurrence is not replaced. So, how can I replace all the occurrences of a given string? Let’s check the next example:
- We can also define the regular expression and the function will replace all occurrences matching that regular expression.
See the below example, where we defined a regular expression to replace string globally.
let str = "Mr Bean has a green Apple and a red Apple" const regex = "/Apple/ig" let result = str.replace("Apple", "Strawberries") console.log(result)123456let str = "Mr Bean has a green Apple and a red Apple" const regex = "/Apple/ig"let result = str.replace("Apple", "Strawberries") console.log(result)Run the above example and see the results.
Replace string with regular expression in JavaScript - Basically, the regular expression is used to match patterns. To replace all occurrences of any string, we can use
replaceAll()
function.The below example uses the replaceAll() function in javascript.
let str = "Mr Bean has a green Apple and a red Apple" let result = str.replaceAll("Apple", "Strawberries") console.log(result)12345let str = "Mr Bean has a green Apple and a red Apple" let result = str.replaceAll("Apple", "Strawberries") console.log(result)Run the above code and see the results.
Replace all matching strings in JavaScript
In this tutorial, we have discussed a few examples to replace a string in javascript.