JavaScript String replace() Method

The replace function in JavaScript is utilized to substitute a portion of a provided string with a different substring. It scans a specified regular expression within the string and performs a replacement when a match is found.

To replace all matched elements in JavaScript using the replace method, we can employ the global search modifier. By utilizing this modifier, all occurrences will be replaced, as opposed to just the first match. Additionally, JavaScript offers an ignore flag to enable case-insensitive replacements with this method.

Syntax

The syntax for the replace method is as follows:

Example

string.replace(originalStr,newStr)

Parameter

The variable originalStr denotes the string that will undergo a search and replace operation.

The variable newStr denotes the string that will substitute the string being searched for.

Return

A fresh string is produced containing the designated substitution.

JavaScript String replace Method Examples

Let's see some examples of the replace method.

Example 1: Replacing a substring

Let's explore a basic illustration of how to substitute a part of a string.

Example

Example

let str="Example";  

console.log(str.replace("Example","Script"));

Output:

Output

ScriptTech

Explanation:

In the previous instance, a variable called str was established using the let keyword. A string was substituted utilizing the .replace function. In this scenario, JavaScript substituted "C# Tutorial" with "ScriptTech".

Example 2: Replacing a regular expression using the global search modifier

In this instance, we will substitute a regular expression with the global search flag.

Example

Example

let str="Learn Node.js on Example. Node.js is a well-known JavaScript framework.";  

console.log(str.replace(/Node.js/g,"AngularJS"));

Output:

Output

Learn AngularJS on Example. AngularJS is a well-known JavaScript framework.

Explanation:

In the previous demonstration, a variable called str was created with the let keyword and initialized with a string value. The substitution of Node.js with AngularJS was executed by applying the global search flag.

Example 3: Replacing a regular expression without using the global search

In this instance, we will substitute a regular expression without utilizing global search functionality.

Example

Example

let str="Learn Node.js on Example. Node.js is a well-known JavaScript framework.";  

console.log(str.replace(/Node.js/,"AngularJS"));

Output:

Output

Learn AngularJS on Example. Node.js is a well-known JavaScript framework.

Explanation:

In the provided instance, we have initialized a variable called str with the use of the let keyword. Instead of employing global search, we have updated a regular expression to substitute solely the initial occurrence.

Example 4: Case-Sensitivity of the replace Method

The replace method in JavaScript is sensitive to letter case. Let's explore a demonstration showcasing the case-sensitive nature of the replace method.

Example

Example

var str="Learn Node.js on Example. Node.js is a well-known JavaScript framework.";  

console.log(str.replace(/Node.JS/g,"AngularJS"));

Output:

Output

Learn Node.js on Example. Node.js is a well-known JavaScript framework.

Explanation:

In the example provided, a variable called str has been declared. The attempt was made to substitute Node.js with AngularJS. However, due to a case discrepancy where Node.js was written as Node.JS, the replace function failed to replace Node.js with AngularJS. This illustrates how the replace method is sensitive to letter case.

Example 5: Ignoring the case-sensitive behaviour of the replace method

By utilizing the ignore flag modifier, we have the ability to disregard the case-sensitive nature of the replace method. Let's delve into this concept through an illustrative example.

Example

Example

let str=" Learn Node.js on Example. Node.js is a well-known JavaScript framework.";  

console.log(str.replace(/Node.JS/gi,"AngularJS"));

Output:

Output

Learn AngularJS on Example. AngularJS is a well-known JavaScript framework.

Explanation:

In the example above, a variable called str has been defined using the let keyword. By utilizing the ignore case flag, the replace method is able to find 'Node.js' without considering case sensitivity and then display the result on the console.

Conclusion

The replace function is a valuable feature of strings in JavaScript, providing a convenient way to substitute one string with another. This method is commonly utilized when working with strings. Within this guide, we have explored the replace method extensively and presented multiple illustrative instances of its application.

The purpose of the replace method in JavaScript is to find a specified value in a string and replace it with another value. This method is commonly used to replace all occurrences of a specified value within a string.

Within JavaScript, the replace method is employed to substitute a substring or character with another string within a given string. Following this substitution, a fresh string is generated containing the altered substring or character. After utilizing the replace method with a string, is the original string altered?

When using the replace method in JavaScript, the original string remains unaltered. Instead, a new string is generated where every occurrence of the specified old value is substituted with the new value.

  1. Is the replacement done for every instance within the string?

When a value is substituted, solely the initial occurrence is altered. To substitute all occurrences, a regular expression with the g modifier must be employed.

  1. Is it viable to substitute the final comma within a string in JavaScript utilizing the replace function?

Certainly, it is possible to substitute the final comma in a string by utilizing the replace function in conjunction with a regular expression pattern.

Input Required

This code uses input(). Please provide values below: