Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I find and replace text in a string using JavaScript?
Asked on Feb 23, 2026
Answer
To find and replace text in a string using JavaScript, you can use the "replace" method. This method allows you to replace a specified value with another value in a string.
<!-- BEGIN COPY / PASTE -->
const originalString = "Hello, world!";
const newString = originalString.replace("world", "JavaScript");
console.log(newString); // Output: "Hello, JavaScript!"
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The "replace" method is used on the "originalString".
- The first argument is the substring you want to find ("world").
- The second argument is the string you want to replace it with ("JavaScript").
- This method returns a new string with the replacement, leaving the original string unchanged.
Recommended Links:
