Skip to main content

How to Replace All Backslashes in a String in JavaScript

Replacing backslashes (\) in a string is a common task, especially when dealing with Windows file paths or escaping sequences. Because the backslash is the escape character in JavaScript strings, you have to handle it with special care.

This guide will demonstrate the modern and most readable method for this task using String.prototype.replaceAll(). We will also cover the classic approach using String.prototype.replace() with a regular expression.

The Core Problem: The Backslash is an Escape Character

In a JavaScript string, the backslash (\) is used to give special meaning to the character that follows it (e.g., \n for a newline, \t for a tab). To represent a literal backslash, you must "escape" it with another backslash.

Example of problem:

// This string actually contains "a", a newline, and "b".
let myString = 'a\nb';

// To create a string with a literal backslash, you must double it.
let pathString = 'C:\\Users\\Test';
console.log(pathString);

Output:

C:\Users\Test
note

This escaping rule applies whenever you are searching for a backslash in a string or a regular expression.

The replaceAll() method is the most direct and readable way to replace all occurrences of a substring. To find a literal backslash, you must pass an escaped backslash ('\\') as the search value.

Problem: you need to replace all backslashes in a Windows-style path with forward slashes for URL compatibility.

// Problem: Convert '\' to '/'.
let windowsPath = 'C:\\Users\\Test\\Documents';

Solution:

let windowsPath = 'C:\\Users\\Test\\Documents';

// The first argument '\\' represents a single, literal backslash.
let webPath = windowsPath.replaceAll('\\', '/');

console.log(webPath);

// To remove all backslashes, just replace with an empty string.
let noBackslashes = windowsPath.replaceAll('\\', '');
console.log(noBackslashes);

Output:

C:/Users/Test/Documents
C:UsersTestDocuments
note

This is the recommended best practice for its clarity and simplicity.

The Classic Method: replace() with a Global Regex

Before replaceAll() was widely available, the standard method was to use replace() with a regular expression and the global (g) flag. This method is equally effective.

Solution: inside a regular expression, the backslash is also an escape character, so you must escape it there as well.

let windowsPath = 'C:\\Users\\Test\\Documents';

// The regex /\\/g finds all literal backslashes globally.
let webPath = windowsPath.replace(/\\/g, '/');

console.log(webPath);

Output:

C:/Users/Test/Documents

Observe that:

  • / ... /g: The / characters are the regex delimiters, and g is the global flag, which is essential for replacing all occurrences.
  • \\: Inside the regex, the first backslash escapes the second one, so the engine searches for a single, literal backslash.

You can also achieve this by splitting the string into an array at each backslash and then joining the array back together with a new separator.

let windowsPath = 'C:\\Users\\Test\\Documents';

// 1. Split the string into an array of parts.
let parts = windowsPath.split('\\'); // Pass an escaped backslash
// `parts` is now ['C:', 'Users', 'Test', 'Documents']

// 2. Join the parts back together with a forward slash.
let webPath = parts.join('/');

console.log(webPath);

Output:

C:/Users/Test/Documents
note

While this works, it's less direct and can be less performant than replaceAll() or replace(), as it requires creating an intermediate array.

Conclusion

Replacing backslashes in a JavaScript string is simple, as long as you remember to escape the backslash character itself.

  • The most modern and recommended best practice is to use replaceAll() with an escaped backslash: str.replaceAll('\\', '/').
  • The classic and equally powerful alternative is to use replace() with a global regular expression: str.replace(/\\/g, '/').
  • The split() and join() method is a valid but generally less direct and less efficient approach.