By Chris Sev and Matt Abrams
Replacing text in strings is a common task in JavaScript. In this article, you’ll look at using replace
and regular expressions to replace text.
Normally JavaScript’s String replace()
function only replaces the first instance it finds in a string:
const myMessage = 'this is the sentence to end all sentences';
const newMessage = myMessage.replace('sentence', 'message');
console.log(newMessage); // this is the message to end all sentences
In this example, only the first instance of sentence
was replaced.
If you want JavaScript to replace all instances, you’ll have to use a regular expression using the /g
operator:
const myMessage = 'this is the sentence to end all sentences';
const newMessage = myMessage.replace(/sentence/g, 'message');
console.log(newMessage); // this is the message to end all messages
This time both instances are changed.
In addition to using the inline /g
, you can use the constructor function of the RegExp
object:
const myMessage = 'this is the sentence to end all sentences';
const newMessage = myMessage.replace(new RegExp('sentence', 'g'), 'message');
console.log(newMessage); // this is the message to end all messages
To replace special characters like -/\^$*+?.()|[]{})
, we’ll need to use a backslash to escape them.
Here’s an example. Given the string this\-is\-my\-url
, let’s replace all the escaped dashes (\-
) with an unescaped dash (-
).
You can do this with replace()
:
const myUrl = 'this\-is\-my\-url';
const newUrl = myMessage.replace(/\\-/g, '-');
console.log(newUrl); // this-is-my-url
Alternatively, use new Regexp()
:
const myUrl = 'this\-is\-my\-url';
const newUrl = myUrl.replace(new RegExp('\-', 'g'), '-');
console.log(newUrl); // this-is-my-url
In this second example, you don’t have to use a backslash to escape the backslash.
In this article, you saw how to replace single instances, multiple instances, and how to handle strings with special characters.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Founder of scotch.io. Slapping the keyboard until good things happen.
Supporting the open-source community one tutorial at a time. Former Technical Editor at DigitalOcean. Expertise in topics including Ubuntu 22.04, Ubuntu 20.04, CentOS, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.