Borislav Hadzhiev
Tue Oct 12 2021·3 min read
Photo by Ian Schneider
To replace all backslashes in a string:
replaceAll()
method, passing it a string containing two
backslashes as the first parameter and the replacement string as the second.replaceAll
method will return a new string with all backslashes
replaced by the provided replacement.const str = 'a\\b\\c'; const replaced = str.replaceAll('\\', '|'); console.log(replaced); // 👉️ a|b|c
We passed the following parameters to the String.replaceAll method:
In the example we replace each backslash with a pipe, however you can provide any replacement string that suits your use case, e.g. a hyphen:
const str = 'a\\b\\c'; const replaced = str.replaceAll('\\', '-'); console.log(replaced); // 👉️ a-b-c
The replaceAll
method does not mutate the original string, it returns a new
string with all matches replaced. Strings are immutable in JavaScript.
replaceAll
method is not supported in Internet Explorer versions 6-11. If you need to support the browser, use the replace
method instead.To replace all backslashes in a string:
replace()
method, passing it a regular expression that matches all
backslashes as the first parameter and the replacement string as the second.replace
method will return a new string with all backslashes replaced.// Supported in IE 6-11 const str = 'a\\b\\c'; const replaced = str.replace(/\\/g, '_'); console.log(replaced); // 👉️ a_b_c
We passed the following parameters to the String.replace method:
The regular expression is quite hard to read and would take a second even for a seasoned developer.
If you ever need help reading a regular expression, check out this regex cheatsheet from the MDN docs.
We use the g
(global) flag because we want to match all backslashes in the
string and not just the first occurrence.
replace
method does not change the original string, it returns a new string with one or more matches replaced.The regular expression is quite difficult to read, so here's an alternative approach, which also is supported by Internet Explorer.
To replace all backslashes in a string:
split()
method on the string, passing it a string containing two
backslashes.split
method will return an array containing the divided substrings.join()
method on the array, passing it the replacement string.join
method returns a new string by concatenating the array elements
with the provided separator.const str = 'a\\b\\c'; const replaced = str.split('\\').join('-'); console.log(replaced); // 👉️ a-b-c
The String.split method returns an array containing the substrings, split on the backslashes.
const str = 'a\\b\\c'; const split = str.split('\\') console.log(split) // 👉️ ['a', 'b', 'c']
The last step is to use the Array.join method to join the array elements with a provided separator.
In the example we use a dash, however you can use any string that suits your use case.
replaceAll
method is my preferred solution for this problem, because it's quite easier to read than the other 2 approaches. However, if you need to support Internet Explorer the other methods get the job done.