Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. JavaScript

How to Remove a Character From a String in JavaScript

by
Difficulty:BeginnerLength:ShortLanguages:

In this quick article, we’ll see how you can remove a character from a string in JavaScript. I'll show you a few different ways to do this.

Along with HTML and CSS, JavaScript is one of the core technologies of the web. The majority of websites use it, and all modern web browsers support it without the need for plugins. It's got a huge community, and the ecosystem is evolving rapidly. In this series, we’re discussing different tips and tricks that will help you in your day-to-day JavaScript development.

In JavaScript, strings have lots of different properties and methods that we can use in order to transform them or glean useful information from them. In fact, there are often many different ways to do the same thing with a string. Today, we’ll discuss a few different ways to remove a specific character from a string.

The replace Method

In JavaScript, the replace method is one of the most frequently used methods to remove a character from a string. Of course, the original purpose of this method is to replace a string with another string, but we can also use it to remove a character from a string by replacing it with an empty string.

Let’s go through the following example to understand how it works.

In the above example, the strWebsiteName string variable contains the \n character, and we want to remove it.

Thus, we’ve used the replace method to achieve it. The replace method takes two arguments. The first argument is a string which you want to replace in the source string, and the second argument is a string which will be replaced with the matched string. In our case, we want to remove the matched string, so we’ve supplied an empty string in the second argument. So the overall effect is that the matched string will be removed from the source string, and the strNewWebsiteName variable will contain the resulting string.

It’s important to note that the replace method only replaces the first occurrence of a string in the source string. So if you want to remove all occurrences of a specific character from the source string, you need to use a regular expression in the replace method. And, that’s what we'll discuss in the very next section!

The replace Method With a Regular Expression

In this section, we’ll discuss how you can use a regular expression to remove every instance of a specific character from a string with the replace method.

Let’s go through the following example to understand how it works.

In the above example, we’ve used a regular expression in the first argument of the replace method. It will remove all occurrences of the \n character from the source string.

In fact, when you’re using a regular expression with the replace method, you can use a callback function to process the replacement, as shown in the following example.

Instead of directly replacing the \n character with an empty string, we’ve used a callback function in the second argument of the replace method. The return value of the callback function should be the value which you want to replace the matched string with. This can be helpful for more sophisticated kinds of replacements.

So that’s how you can use the replace method with a regular expression to remove all occurrences of a specific character from the source string.

The split Method

In this last section, we’ll see how you can use the split method to remove a character from a string in JavaScript. In fact, it’s a combination of the split and join methods which does the trick.

Let’s go through the following example.

Firstly, we’ve used the split method to split the string by the \n character, and it would return an array of strings. After that, we’ve used the join method to join all the pieces with an empty string, hence the end result is that it removes the \n character from the source string. So that’s how you can use the split method to remove characters from a string.

Conclusion

Today, we discussed various methods that you can use to remove a character from a string in JavaScript.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.