Last updated: Mar 1, 2024
Reading timeยท4 min
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.
const str = 'bobby\\hadz\\com'; const replaced = str.replaceAll('\\', '-'); console.log(replaced); // ๐๏ธ 'bobby-hadz-com'
If you need to remove all backslashes from the string, use the following code sample instead.
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
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.
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.
String.split()
This is a two-step process:
split()
method to split the string into an array, on each
backslash.join()
method to join the array into a string with a separator.const str = 'bobby\\hadz\\com'; const replaced = str.split('\\').join('_'); console.log(replaced); // ๐๏ธ 'bobby_hadz_com'
We used the String.split()
method to split the string on each backslash.
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:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An 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.
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.
String.replace()
Alternatively, you can use the String.replace()
method.
const str = 'bobby\\hadz\\com'; const replaced = str.replace(/\\/g, '_'); console.log(replaced); // ๐๏ธ 'bobby_hadz_com'
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A 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.
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.
You can learn more about the related topics by checking out the following tutorials: