Functions - Replace Function in JavaScript
Posted by Superadmin on May 05 2023 04:08:36

Replace Function in JavaScript

By Shalu SharmaShalu Sharma
  

Replace Function in JavaScript

Introduction to Replace Function in JavaScript

JavaScript provides us many inbuilt functions which also include replace function. Replace function in JavaScript is used to replace string. It is used to replace a given string or some part of the string. But in this function also original string will not change. Replace() method search for a specified value into the string or a regular expression and replace the string without modifying the original string because it always returns the new string.

Syntax

Below is the syntax:

string.replace(A, B)

This replace method takes two parameters lets discuss them in details:

Parameters:

or

var newString = str.replace(reg-expression|str, newSubstr|functionTocall)

Here we will discuss the below syntax in detail.

We can also say that this variable as a search value and a new value. Both these parameters are required they are not optional. It uses JavaScript Version i.e ECMAScript 1.

Example:

To show basic usage of replace() function.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Prerss the bnutton to replace content "abc" with "black" in the content below </p>
<p id="demo">This abc will be reaplce by balck from abc black.</p>
<button onclick="myFunction1()">Try to press!</button>
<script>
function myFunction1() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/abc/g, "wooooo");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Output:

replace function in javascript 1

replace function in javascript 2

How Replace Function Works in JavaScript?

Replace() function is used to replace the string as the name suggests it replaces whole or some string depending upon the input or pattern we pass. This pattern can be anything like regular expression or a string value itself. But in this we have to note one thing i.e. if the pattern that we are passing is a string value then only the first occurrence of the string will be replaced. For all the occurrence to get replaced we need to pass regular expression which will find and replace all.

But in all this, our original string will still be unchanged. So basically it does not change the string object on which it is called. It will just return the new string. If we want to perform a global replace and search then we need to include the g switch while using the regular expression.

Replacement string contains some special character patterns:

While using replace() method we can also mention this function parameter as the second parameter but in this case, this function will be invoked only after the match with the corresponding string has been performed.

We have the following argument for function as follows:

Examples of Replace Function in JavaScript

Below are examples of Replace Function in JavaScript:

Example #1

Using regular expression inside replace() function.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press button to see use of replace function </p>
<p id="demo">Some text will be replaced on click...</p>
<button onclick="myFunction1()">Try to press!</button>
<script>
function myFunction1() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/.../i, 'RED');
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Output:

example 1

replace function in javascript

Example #2

To show global replace.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press button to see use of replace function </p>
<p id="demo">Black will be replace by another word.</p>
<button onclick="myFunction1()">Try to press!</button>
<script>
function myFunction1() {
var re = /Black/gi
var str = document.getElementById("demo").innerHTML;
var res = str.replace(re, 'yellow');
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Output:

example 2

replace function in javascript 6

Example #3

To show switching of words by using regular expression

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press button to see use of replace function </p>
<p id="demo">Words will be reverse in this example.(first two)</p>
<button onclick="myFunction1()">Try to press!</button>
<script>
function myFunction1() {
var re = /(\w+)\s(\w+)/;
var str = document.getElementById("demo").innerHTML;
var res = str.replace(re, '$2, $1');
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Output:

example 3

replace function in javascript 8

Example #4

We are calling functions to perform changes. Converting a string to uppercase.

Code:

<!DOCTYPE html>
<html>
<body>
<p>By clicking the button below word will be in upper case</p>
<p id="demo">Here we are chnaging some words to uppercase by clicking the button.</p>
<button onclick="myFunction1()">Try press!</button>
<script>
function myFunction1() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/are|uppercase|button/gi, function (x) {
return x.toUpperCase();
});
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Output:

replace function in javascript 9JPG

replace function in javascript 10JPG

Conclusion

JavaScript replace() function is basically used to replace the string or substring bypassing string as a value or regular expression. We can also call functions from inside to perform more changes on the string like uppercase, lowercase, etc. The main advantage of using this replace function is that it is never changing our original string. It will always return us a new string.