Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript String replace() Method

  • 19 Aug, 2025
  • Com 0
JavaScript String replace() method

JavaScript String replace() is a built-in method that returns a new string by replacing some or all parts of an existing string that match a specified pattern (string or RegExp) with a replacement value (string).

Replacing a string in JavaScript

let input = "Today is a Saturday, I love Saturday";

let output = input.replace("Saturday", "Sunday")

console.log(output);

// Output: Today is a Sunday, I love Saturday

In this code, only the first “Saturday” is replaced by “Sunday” because string patterns replace only the first match; subsequent matches (if any) are ignored. It does not modify the original string since it is immutable.

Syntax

string.replace(search_value, new_value)

Parameters

Argument Description
search_value (required)

It represents a value to search for.

If it is a string, it matches and replaces only the first occurrence (case-sensitive).

If it is a RexExp object, it enables advanced pattern matching.

If not a RegExp or custom object, it is coerced to a string.

new_value (required) It is the value to insert in place of the match(es).

It can be a string or a function.

Replace using Regular Expressions

Replacing using Regular Expressions

 

If you don’t know the specific string but you know the pattern of it, you can use regular expressions to target that pattern and replace it.

Matching an exact substring is good, but we often need to match a pattern instead. It can quickly be done using the regular expression as the first argument.

const text = "cat dog tiger";

console.log(text.replace(/cat/, "cow"));

// Output: "cow dog tiger"

RegExp with global flag (All Matches)

RegExp with global flag (All Matches) in JavaScript

If you want to replace all occurrences, pass the “g” flag to the Regular Expression.

const text = "cat dog cat cat tiger";

console.log(text.replace(/cat/g, "cow"));

// Output: cow dog cow cow tiger

The above program shows that it replaced all the “cat” strings with “cow” strings.

Case-Insensitive replacement

String.replace() is case-sensitive

By default, the replace() method is case-sensitive. Meaning, it treats “Web” and “web” strings differently.

const text = "Apple apple";

console.log(text.replace(/apple/g, "orange"));

// Output: Apple orange

Even after using global replacement, it does not replace “Apple” because it is case-sensitive.

To replace a specific string regardless of its case sensitivity, pass the “i” argument while passing a RegExp.

Ignoring cases in str.replace() method

const text = "Apple apple";

console.log(text.replace(/apple/gi, "orange"));

// Output: orange orange

In the above code, we passed “/gi”, where “g” stands for global and “i” stands for ignore cases.

Replacement string with special patterns

const str = "Price: $100";

const result = str.replace(/\$(\d+)/, "USD $1");

console.log(result);

// Output: Price: USD 100

Here $1 refers to the first captured group (\d+).

Capturing Groups with $n

Let’s capture two grouped characters “te” ($1) and “st” ($2), and swap them.

const str = "test";

const result = str.replace(/(te)(st)/, "$2$1");

console.log(result);

// Output: Price: stte

In this program, we swapped the substrings of the “test” string. The first “te” and last “st” have been swapped, and the final string is “stte”. 

Function as a Replacement

We can pass a replacement value as a function like this:

const str = "aabbcc"

const output_str = str.replace(/./g, (match) => match.toUpperCase())

console.log(output_str);

// Output: AABBCC

In this code, we pass a function as a value, which is called for each match (due to g), uppercasing characters.

Replacing an array of strings

What if we have an array of strings and we want to replace the string value of a specific element?

In that case, we need to go through each element one by one using an array.map() method. Once we find an element that needs to be replaced, we will use the string.replace() method and replace that specific element’s value with the provided value, transform, and return the array.

Visual Representation of Replacing an Array of Strings

let ingredients = ['Y-MEN', 'Avengers', 'Loki'];

ingredients = ingredients.map(ingredient => {
    return ingredient.replace('Avengers', 'Revengers');
});

console.log(ingredients);

// Output: ['Y-MEN', 'Revengers', 'Loki']

That’s all!

Post Views: 15
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Replace Elements in a Python List
How to Remove an Element from a Set If It Exists in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend