JavaScript - String replace() Method
In this post, we will discuss about String replace() method in JavaScript. replace() is a method used to replace the first occurrence of a given string if it matches.
Let us create the String for demonstration.
var string1='This is a string with global scope';
let string2='string created with block scope';
const string3='this is a constant string never gets changed';
console.log(string1);
console.log(string2);
console.log(string3);
Output:
This is a string with global scope
string created with block scope
this is a constant string never gets changed
String replace():
replace() used to replace the first occurrence of a String with the given string if it contains matching value.
replace() syntax:
String.replace(findValue,replaceValue)
Where 'findValue' is used to find the string contains if it found replaces with 'replaceValue' for only first occurrence. Its not going to change the original value of the string but it returns new string with changes. We will see how to replace the string with the given string in the examples.
replace() example 1:
create a string declaration with sentence and replace any part of the sentence with new string.
var string1='This is a string with global scope';
let string2='string created with block scope';
const string3='this is a constant string never gets changed';
console.log(string1.replace('This','It'));
console.log(string2.replace('created','declared'));
console.log(string3.replace('this','It'));
Execution Steps:
- Copy the above example code.
- Open the browser and press(ctrl+shift+I).
- Go to the console tab.
- Paste the code into console and press enter button.
Output:
It is a string with global scope
string declared with block scope
It is a constant string never gets changed
replace() example 2:
In this example, we will print the given string and replaced string to check original string is changed.
var string1='This is a string with global scope';
let string2='string created with block scope';
const string3='this is a constant string never gets changed';
const string1replace = string1.replace('This','It');
const string2replace = string2.replace('created','declared');
const string3replace = string3.replace('this','It');
console.log('original string = '+string1);
console.log('replaced string = '+string1replace);
console.log('original string = '+string2);
console.log('replaced string = '+string2replace);
console.log('original string = '+string3);
console.log('replaced string = '+string3replace);
Execution Steps:
- Copy the above example code.
- Open the browser and press(ctrl+shift+I).
- Go to the console tab.
- Paste the code into console and press enter button.
Output:
original string = This is a string with global scope
replaced string = It is a string with global scope
original string = string created with block scope
replaced string = string declared with block scope
original string = this is a constant string never gets changed
replaced string = It is a constant string never gets changed