A trash can with the word 'World' inside of it, illustrating how to remove a character from a string in JavaScript.

A common form of string manipulation in JavaScript is to remove a character from a string. Let’s explore all of the ways we can do this in JavaScript.

What is Replace in JavaScript?

You’re probably thinking that there is a String function called remove, aren’t you? Unfortunately tere isn’t a remove String method in JavaScript, however, there is the replace method.

The replace method in JavaScript takes two arguments:

  • The characters to be replaced.
  • The characters to replace it with.

In fact, the replace method is very power, as it allows us to switch a string or character with another string or character. Let’s take a look at a simple example.

Using Replace to Remove a Character from a String in JavaScript

const greeting = "Hello my name is James"; const omittedName = greeting.replace('James', '');

The example above declares a new constant, named greeting, which is of type string.

Learn how to extract strings from other strings using the substring method.

We then run the replace method on that string, which replaces the characters ‘James’ inside of the greeting constant with a blank space.

Simple! We’re using JavaScript to remove the ‘James’ character from a string.

Example: Remove Multiple Characters from a String in JavaScript

What if you had a scenario where you wanted to replace multiple instances of the name character from a string? The code example above would not replace all of the characters: ‘James’ from a string, only the first set of characters that match ‘James’.

Let’s explore how we can remove multiple characters from a string in JavaScript using a regular expression that uses the global modifier to target all occurances of that string.

const description = "Upmostly teaches React and JavaScript tutorials. JavaScript is a scripting language."; const newDescription = greeting.replace(/JavaScript/g, '...');

The example code above creates a new constant variable named description (which is a description of this very site!). We then perform the replace method on it, but this time, use the global modifier in a regular expression.

This modifer, /g targets all strings inside of the description string that match the first argument.

When to Use Replace in JavaScript?

The replace method is another way to manipulate strings in JavaScript. There could be a scenario whereby you have connected to a database, or are querying an API to return external data that someone else designed and architected.

Therefore, that data is likely going to be formatted in a particular way and may not be in the format that you want or need. That’s when you’ll need to perform some string manipulation on it.


📬 The Monthly Upmostly Newsletter

One email a month, packed with the latest React tutorials, delivered straight to your inbox.
Zero spam, just great content. Unsubscribe at any time.
James King headshot
👋 Hey, I'm James King
My tutorials help 60,000+ developers learn React and JavaScript every month. If you'd like to receive a friendly email once in a while of all new React tutorials, just pop your email above! I appreciate the support!

💬 Leave a comment

Your email address will not be published. Required fields are marked *

We will never share your email with anyone else.