Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the replace() method in JavaScript? 
3.
Introduction: Javascript String replace() Method
3.1.
replace() Syntax
3.2.
replace() Parameters
3.3.
replace() Return Value
4.
Specifying a String as a Replacement
5.
Specifying a Function as Replacement
5.1.
replace() Syntax
5.2.
replace() Parameters
6.
Frequently Asked Questions
6.1.
What is replace () in Javascript?
6.2.
How to replace all words in Javascript?
6.3.
How to replace variable in JavaScript?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript String replace() Method

Author Sinki Kumari
0 upvote

Introduction

JavaScript is a very popular and commonly used scripting language used for web development. It is not only versatile and but it can also be used to perform a wide range of tasks, including replacing text in a string. 

JavaScript replace()

In this blog, we will learn about the syntax of the JavaScript replace function along with the various ways in which we can use it. We will also look at some commonly asked questions on the same.

What is the replace() method in JavaScript? 

The replace() method in JavaScript is a string method used to find and replace a specific substring or pattern in a given string with a new substring. It does not modify the original string but returns a new string with the replacements made.

Introduction: Javascript String replace() Method

replace() Syntax

The syntax for using the replace() method is as follows: 

string.replace(searchValue, replaceValue); 

replace() Parameters

  • string is the original string
     
  • searchValue is the substring that you want to replace
     
  • replaceValue is the new substring that you want to insert in place of the search value

replace() Return Value

The new string after replacing the search value with the replace value of the substring.

Specifying a String as a Replacement

The following replacement patterns are included with string replacement:

Pattern

Function

$$ This inserts a "$".
$& This inserts the matched substring.
$' This inserts the portion of the string that follows the matched substring.
$` This inserts the portion of the string that precedes the matched substring.
$n This inserts the nth (1-indexed) capturing group where n is a positive integer less than 100.
$<Name> This inserts the named capturing group where Name is the group name.
$_  This inserts the entire input string
$*  This inserts the entire match,including any text before or after it 
$+ This inserts the last matched group

Specifying a Function as Replacement

In JavaScript, the replace() function can also accept a function as the second argument, which is called for each match and returns the replacement string. This allows for more complex and dynamic string replacements. 

replace() Syntax

string.replace(searchValue|regexp, function(match, p1, p2, ..., offset, string) {
  // Function body
})

replace() Parameters

  • match: The matched substring.
     
  • p1, p2, ...: The captured groups, if any.
     
  • offset: The index of the matched substring within the original string.
     
  • string: The original string.

Frequently Asked Questions

What is replace () in Javascript?

replace() is a JavaScript string method used to replace specified characters or patterns in a string with new ones.

How to replace all words in Javascript?

To replace all occurrences, use a regular expression with the global flag: str.replace(/word/g, 'newWord').

How to replace variable in JavaScript?

Replace a variable's value by assigning a new value: variableName = newValue;.

Conclusion

The JavaScript replace() method is a versatile tool that allows developers to replace text in a string with new text or modify the existing text in a string. It can replace a single occurrence of text, multiple occurrences of text, and many other things. 

Understanding how to use the replace() method is essential for any JavaScript developer who wants to manipulate strings effectively.

Check out the following for a better understanding:

Check out some amazing Guided Paths on topics such as the Basics of javascriptBasics of angularBasics of reactBasics of nodeFundamentals of react native, etc., along with some Contests and Interview Experiences only on Coding Ninjas Studio

Do check out The Interview Guide for Product Based Companies, as well as some of the Popular Interview Problems from Top companies like AmazonAdobeGoogle, etc., on Coding Ninjas Studio.

Live masterclass