Featured Article

W3Schools JavaScript Replace Text Tutorial Guide

Adam Jul 13, 2026

W3Schools, a renowned web development platform, offersummies and pro-level coders alike a comprehensive range of tutorials and references on JavaScript. One of the most fundamental operations in JavaScript is string manipulation. Amongst various operations, replacing text within a string is a common and essential task. Today, we will demystify `replace()` method, discussing its syntax, usage, and important notes.

How to Disable Copy Text in Javascript & CSS
How to Disable Copy Text in Javascript & CSS

The `replace()` method, as the name suggests, is used to replace specified keywords within a JavaScript string. But before we dive deep, let's review the method's syntax:

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Syntax of Replace Method

The `replace()` method can be used in two ways – simple and regular expression-based. The syntax for both is slightly different.

JavaScript Reserved Words: What You Need to Know πŸš€
JavaScript Reserved Words: What You Need to Know πŸš€

The simple syntax is as follows:

str.replace(searchValue, replacementValue)

The `str` is the string on which we want to perform the operation. `searchValue` is the text we want to replace, and `replacementValue` is the text we want to insert at that location.

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

Simple Replace

In its simpler form, `replace()` method is quite straightforward. It will replace the first occurrence of the `searchValue` in the string with `replacementValue`.

Let's view an example:

JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘

let str = 'Hello, World!'; let res = str.replace('World', 'JavaScript'); console.log(res);

The output here will be `Hello, JavaScript!`

Case Sensitivity

Notice that the `replace()` method is case-sensitive. If your searchValue includes capital letters, it would replace only those, not the lowercase version of the same word. Adjust your input to match this, or use a regular expression for case-insensitive match.

Javascript Add Text To Image
Javascript Add Text To Image

Replace All Occurrences

In many cases, you might want to replace all occurrences of a certain word, not just the first one. For this, we need to switch to regular expressions.

an image of a web page with different types of text
an image of a web page with different types of text
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
an info sheet with different types of web pages
an info sheet with different types of web pages
Javascript String Methods
Javascript String Methods
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Recursion Simplified
JavaScript Recursion Simplified
JavaScript | Chapter 2 πŸ“–| Part 1 ✨
JavaScript | Chapter 2 πŸ“–| Part 1 ✨
Javascript splice
Javascript splice
Java script string method
Java script string method
How To Create Rainbow Text Using CSS JS
How To Create Rainbow Text Using CSS JS
Update HTML in DOM: Change Text Live
Update HTML in DOM: Change Text Live
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
an image of a web page with text
an image of a web page with text
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
Javascript Loops & Iterations
Javascript Loops & Iterations
10 Must-Know JavaScript Shorthand Techniques
10 Must-Know JavaScript Shorthand Techniques
Using switch Statements in JavaScript
Using switch Statements in JavaScript
the different types of computers are shown in this diagram, and each one has its own language
the different types of computers are shown in this diagram, and each one has its own language
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet

The syntax for using a regular expression with `replace` is as follows:

str.replace(/searchValue/g , replacementValue)

The `/g` modifier stands for global, instructing JavaScript to replace all occurrences not just the first one.

Global Replace

Let's modify our previous example:

let str = 'Hello, World! Hello again, World!'; let res = str.replace(/World/g , 'JavaScript'); console.log(res);

The Unexpected Behavior

You might face an unexpected behavior here. The `replace()` function doesn’t necessarily work as expected when replacing a substring, especially if it’s inside another string that happens to match the regex. It might end up replacing more than expected. To avoid this, the best practice is to use `replace()` with `g` globally only when the `searchValue` is a strictly defined character like a number or symbol, never a word or part of a word.

Finally, remember that the `replace()` behavior is document level. It does not modify the original string but creates a new one. If you want to permanently change a variable, you should reassign it as in the examples above.

Mastering `replace()` is not just coercing strings; it's understanding the basics of JavaScript. Now go out there and impress with your newfound JavaScript prowess! Time to code something amazing.