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 OverflowMDN 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.
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
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
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
JavaScript alternatives
Consider developing for platforms other than the web if you don't want to use JS or TS. More on reddit.com
01:44
How To Replace Part of a String - JavaScript String Replace (In ...
13:34
JavaScript Replace Method Explained: Strings, Regex, & Dynamic ...
03:21
JavaScript Basics - replace() - YouTube
02:20
JS tips: replace all instances in a string WITHOUT regex! (FIX ...
09:20
Using the String.replace() method - JavaScript Tutorial - YouTube
04:42
String Replace with Callback Function, in JavaScript - YouTube
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.
Top answer 1 of 16
211
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.
2 of 16
90
More simply:
city_name=city_name.replace(/ /gi,'_');
Replaces all spaces with '_'!
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.
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.
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...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › RegExp › Symbol.replace
RegExp.prototype[Symbol.replace]() - JavaScript | MDN
July 10, 2025 - If it's a string, it will replace the substring matched by the current regexp.
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