Tutorial

JavaScript Replace All Instances of a String

Draft updated on Invalid Date
author

Chris on Code

JavaScript Replace All Instances of a String

This tutorial is out of date and no longer maintained.

Introduction

I’ve started building out our JavaScript Glossary and when I got to the replace() method, I had to build out a snippet to handle replacing all occurrences of a string in a string.

Only Replaces One

myMessage.replace('sentence', 'message');

https://gist.github.com/chris-sev/7be587f89ba2ee18f105a57a791a2c18

Replaces All with literal regular expression

Normally String replace() only replaces the first instance it finds. If we want JavaScript to replace all, we’ll have to use a regular expression using /g.

myMessage.replace(/sentence/g, 'message');

https://gist.github.com/chris-sev/452b0b9c2ff1d4ddf1ae3449f90ef595

Using RegExp()

In addition to using the inline /g, we can use the constructor function of the RegExp object.

myMessage.replace(new RegExp('sentence', 'g'), 'message');

https://gist.github.com/chris-sev/fcd4396ee879d3ccc306512a59e2608a

Replacing Special Characters

To replace special characters like -/\^$*+?.()|[]{}) we’ll need to use a \ backslash to escape them.

Here we’ll replace all the - in this string with just -. I ran into this when building out the Scotch dashboard with markdown trying to escape all my symbols.

// replace - with -
myUrl.replace(/-/g, '-');

// or with RegExp
myUrl.replace(new RegExp('-', 'g'), '-');

https://gist.github.com/chris-sev/d1d233fb4ff5264cd50b8208a03dcf84

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment
Leave a comment...

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!

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.