You would use the replace method:

text = text.replace('old', 'new');

The first argument is what you're looking for, obviously. It can also accept regular expressions.

Just remember that it does not change the original string. It only returns the new value.

Answer from sdleihssirhc on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
April 12, 2026 - The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
🌐
W3Schools
w3schools.com › jsref › jsref_replace.asp
JavaScript String replace() Method
The replace() method returns a new string with the value(s) replaced.
Discussions

replace - How can I perform a str_replace in JavaScript, replacing text in JavaScript? - Stack Overflow
I want to use str_replace or its similar alternative to replace some text in JavaScript. var text = "this is some sample text that i want to replace"; var new_text = replace_in_javascript... More on stackoverflow.com
🌐 stackoverflow.com
Replace method in Javascript
Hi again :slight_smile: hope all is well, and I’ll get straight into it! I was looking at alternative solutions to a coding challenge I came across on codesignal and came across this solution: function reverseInParent… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
8
0
October 21, 2021
If JavaScript is so terrible, why hasn't it been replaced by something else already?
It can't be directly replaced since that would require coordination of all the major browsers to support a 2nd language. Also, Typescript is doing a great job replacing it and is not terrible. More on reddit.com
🌐 r/AskProgramming
99
31
December 11, 2023
JavaScript alternatives
Consider developing for platforms other than the web if you don't want to use JS or TS. More on reddit.com
🌐 r/webdev
31
0
March 25, 2024
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
April 12, 2026 - The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
🌐
Mimo
mimo.org › glossary › javascript › replace
JavaScript Replace Method: Advanced String Manipulation
The replace() method in JavaScript is a powerful tool for string manipulation.
🌐
David Bushell
dbushell.com › 2024 › 02 › 01 › javascript-string-replace
Thought You Knew String Replace?
Pattern is usually a string or regular expression. Technically it can be any object with a Symbol.replace method (like a RegExp). Replacement is either a string or function that returns a string.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-replace-how-to-replace-a-string-or-substring-in-js
JavaScript Replace – How to Replace a String or Substring in JS
November 7, 2024 - In JavaScript, you can use the replace() method to replace a string or substring in a string. The replace() method returns a new string with the replacement.
Find elsewhere
🌐
TechOnTheNet
techonthenet.com › js › string_replace.php
JavaScript: String replace() method
This JavaScript tutorial explains how to use the string method called replace() with syntax and examples. In JavaScript, replace() is a string method that is used to replace occurrences of a specified string or regular expression with a replacement string.
🌐
DEV Community
dev.to › maafaishal › javascript-stringreplace-useful-cases-3963
JavaScript `string.replace()` useful cases - DEV Community
September 24, 2024 - let str = "Hello World, World!"; let result = str.replace(/world/gi, "JavaScript") // Output: "Hello JavaScript, JavaScript!"
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.replace()
JavaScript String replace() Method
November 4, 2024 - In this tutorial, you'll how to use JavaScript String replace() function to replace a substring in a string with a new one.
🌐
Career Karma
careerkarma.com › blog › javascript › javascript replace(): a step-by-step guide
JavaScript Replace(): A Step-By-Step Guide | Career Karma
December 1, 2023 - To replace text in a JavaScript string the replace() function is used. The replace() function takes two arguments, the substring to be replaced and the new string that will take its place.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Replace method in Javascript - Curriculum Help - The freeCodeCamp Forum
October 21, 2021 - Hi again 🙂 hope all is well, and I’ll get straight into it! I was looking at alternative solutions to a coding challenge I came across on codesignal and came across this solution: function reverseInParentheses(inputString) { while (inputString.includes('(')) { inputString = inputString.replace(/\(([^()]*)\)/, (_, str) => [...str].reverse().join('')); } return inputString; } now, whilst I understand in natural language terms, I don’t understand why they have t...
🌐
Medium
medium.com › @SM196 › javascript-replace-7b081b403268
JavaScript replace(). Let's clear the concept of replace()… | by Soha - JavaScript Developer | Medium
January 17, 2023 - JavaScript replace() Let's clear the concept of replace() and learn where we can use it. Let's get started! The replace() function is a method in JavaScript that is used to replace text in a string
🌐
freeCodeCamp
freecodecamp.org › news › javascript-replace-how-to-use-the-string-prototype-replace-method-js-example
JavaScript Replace – How to Use the String.prototype.replace() Method JS Example
February 8, 2022 - The String.prototype.replace() method searches for the first occurrence of a string and replaces it with the specified string. It does this without mutating the original string. This method works for regular expressions, too, so the item you're ...
🌐
Programiz
programiz.com › javascript › library › string › replace
JavaScript String replace()
To perform the case-insensitive replacement, you need to use a regex with an i switch (case-insensitive search). const text = "javaSCRIPT JavaScript" // the first occurrence of javascript is replaced let pattern = /javascript/i; // case-insensitive search let new_text = text.replace(pattern, "JS"); console.log(new_text) // JS JavaScript // all occurrences of javascript is replaced pattern = /javascript/gi; // case-insensitive and global search new_text = text.replace(pattern, "JS"); console.log(new_text) // JS JS
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-replace-method
JavaScript string replace() Method - GeeksforGeeks
June 26, 2024 - It allows you to search for a specific part of a string, called a substring, and then replace it with another substring.
🌐
CoreUI
coreui.io › blog › how-to-replace-all-occurrences-of-a-string-in-javascript
How to replace all occurrences of a string in JavaScript? · CoreUI
August 31, 2024 - In this example, the global regular expression /hello/g matches all instances of ‘hello’, ensuring that every occurrence is replaced with ‘hi’. This is one of the most common ways to replace multiple occurrences of a substring in JavaScript.
🌐
CodingNomads
codingnomads.com › javascript-replace-string
Guide to JavaScript String Replacement
The .replace() method is the most common way to replace strings in JavaScript. It takes two arguments: the string to replace (the "pattern") and the string to replace it with (the "replacement").