Replace or Remove all Backslashes in a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Replace all backslashes in a String in JavaScript

Use the String.replaceAll() method to replace all backslashes in a string, e.g. const result = str.replaceAll('\\', '-');.

The replaceAll method returns a new string with all matches replaced by the provided replacement.

index.js
const str = 'bobby\\hadz\\com'; const replaced = str.replaceAll('\\', '-'); console.log(replaced); // ๐Ÿ‘‰๏ธ 'bobby-hadz-com'

replace all backslashes in string

The code for this article is available on GitHub

If you need to remove all backslashes from the string, use the following code sample instead.

index.js
const str = 'bobby\\hadz\\com'; const backslashesRemoved = str.replaceAll('\\', ''); console.log(backslashesRemoved); // ๐Ÿ‘‰๏ธ bobbyhadzcom

The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
index.js
const str = 'bobby\\hadz\\com'; const replaced = str.replaceAll('\\', '-'); console.log(replaced); // ๐Ÿ‘‰๏ธ 'bobby-hadz-com'

In the example, we replace all occurrences of a backslash with a hyphen.

The String.replaceAll() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

We had to use two backslashes because the backslash \ is used as an escape character in JavaScript.

By adding a second backslash, we treat the backslash character as a literal backslash \ instead of an escape character.

If you have to do this often, define a reusable function.

index.js
function replaceAllBackslashes(string, replacement) { return string.replaceAll('\\', replacement); } // ๐Ÿ‘‡๏ธ bobby-hadz-com console.log(replaceAllBackslashes('bobby\\hadz\\com', '-')); // ๐Ÿ‘‡๏ธ bobby!hadz!com console.log(replaceAllBackslashes('bobby\\hadz\\com', '!'));

We defined a function that takes a string and a replacement as parameters and replaces all occurrences of a backslash in the string.

Alternatively, you can use the String.split() and Array.join() methods.

# Replace all backslashes in a String using String.split()

This is a two-step process:

  1. Use the split() method to split the string into an array, on each backslash.
  2. Use the join() method to join the array into a string with a separator.
index.js
const str = 'bobby\\hadz\\com'; const replaced = str.split('\\').join('_'); console.log(replaced); // ๐Ÿ‘‰๏ธ 'bobby_hadz_com'

replace all backslashes in string using split

The code for this article is available on GitHub

We used the String.split() method to split the string on each backslash.

index.js
const str = 'bobby\\hadz\\com'; // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com' ] console.log(str.split('\\'));

The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.

The String.split() method takes the following 2 parameters:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn integer used to specify a limit on the number of substrings to be included in the array.

The last step is to use the Array.join() method to all array elements with a separator (replacement for each backslash).

If a value for the separator argument is omitted, the array elements are joined with a comma ,.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

We used two backslashes in the call to split() to escape the second backslash in order to treat it as a literal character and not an escape character.

You can create a reusable function if you have to do this often.

index.js
function replaceAllBackslashes(string, replacement) { return string.split('\\').join(replacement); } // // ๐Ÿ‘‡๏ธ bobby-hadz-com console.log(replaceAllBackslashes('bobby\\hadz\\com', '-')); // // ๐Ÿ‘‡๏ธ bobby!hadz!com console.log(replaceAllBackslashes('bobby\\hadz\\com', '!'));

The function takes a string and a replacement as parameters and replaces all occurrences of a backslash in the string.

# Replace all backslashes in a String using String.replace()

Alternatively, you can use the String.replace() method.

index.js
const str = 'bobby\\hadz\\com'; const replaced = str.replace(/\\/g, '_'); console.log(replaced); // ๐Ÿ‘‰๏ธ 'bobby_hadz_com'

replace all backslashes in string using replace

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

The forward slashes / / mark the beginning and end of the regular expression.

We used two backslashes next to one another. The first backslash is used to escape the second in order to treat the backslash as a literal character and not an escape character.

We used the g (global) flag because we want to match all occurrences of the backslash \ in the string, and not just the first occurrence.

If you have to do this often, extract the logic into a reusable function.

index.js
function replaceAllBackslashes(string, replacement) { return string.replace(/\\/g, replacement); } // ๐Ÿ‘‡๏ธ bobby-hadz-com console.log(replaceAllBackslashes('bobby\\hadz\\com', '-')); // ๐Ÿ‘‡๏ธ bobby!hadz!com console.log(replaceAllBackslashes('bobby\\hadz\\com', '!'));

The function takes a string and a replacement as parameters and replaces all occurrences of a backslash in the string.

Which approach you pick is a matter of personal preference. I'd use the String.replaceAll() method as I find it quite direct and intuitive.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev