Got stumped by this for ages and all the answers kept insisting that the source string needs to already have escaped backslashes in it ... which isn't always the case.

Do this ..

var replaced = str.replace(String.fromCharCode(92),String.fromCharCode(92,92));
Answer from thegajman on Stack Overflow
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-replace-all-backslashes-in-string
Replace or Remove all Backslashes in a String in JavaScript | bobbyhadz
March 1, 2024 - 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. ... Copied!function replaceAllBackslashes(string, replacement) ...
๐ŸŒ
Directory Opus
resource.dopus.com โ€บ off-topic
Find and Replace Backslash in a JavaScript String - Off-Topic - Directory Opus Resource Centre
December 9, 2023 - JavaScript Question This is I think a "pure" JavaScript question not DO really hence posting in Off-Topic. It is related to this post: Rename command, {alias} code and Double backslashes Objective In a JavaScript string variable I have reason to want to replace all instances of backslash \ ...
Discussions

Can't replace backslash in a string with regex
const name = 'AC\\DC'; should work. More on reddit.com
๐ŸŒ r/learnjavascript
11
6
May 2, 2023
php - How to remove backslash from a string in JavaScript - Stack Overflow
I have a string like this: Dialed 9804292145453 A Jana 0sec More on stackoverflow.com
๐ŸŒ stackoverflow.com
html - Removing backslashes from strings in javascript - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to replace backslash in string using javascript? - Stack Overflow
Is there any way to replace backslash without it vanishing? ... The fact that your string literal has a backslash in it, does not mean that your actual string has. Try to print it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ can't replace backslash in a string with regex
r/learnjavascript on Reddit: Can't replace backslash in a string with regex
May 2, 2023 -

I need to replace all backslashes in a string with another symbol. I use regex for that, but it just doesn't work

const name = 'AC\DC'; 
const replaced = icon.replace(/\\/g, "-");

console.log(name);
console.log(replaced);

// ACDC
// ACDC

For example in this string the backslash is not replaced and not even shown when I log the origial string in a console.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 53830887 โ€บ how-to-replace-backslash-in-string-using-javascript
How to replace backslash in string using javascript? - Stack Overflow
var temp = JSON.parse(JSON.stringify('{"FHPosition":"consultant","FDesc":"apr4","FHId":"i:0#.w|spdev\gkr"}').replace("\\","\\\\")) console.log(temp.FHId); Hopefully this helps! ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-globally-replace-a-forward-slash-in-a-javascript-string
JavaScript โ€“ How to Globally Replace a Forward Slash in a String? | GeeksforGeeks
November 26, 2024 - The original string will remain unchanged.Syntaxstr.replace(replac ... In JavaScript, adding a backslash to a JSON string is important to properly escape special characters, ensuring the integrity and correctness of the JSON format for data processing and storage.
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ js_remove_backslash_from_string.php
Master js remove backslash from string (2026): Clean Your Data & Prevent Errors Today!
April 6, 2026 - '': The replacement string, which is an empty string, effectively deleting the matched backslashes. For newer JavaScript environments (ES2021 and later), replaceAll() offers a simpler syntax if you're not using a regular expression, or it still works with global regex.
๐ŸŒ
Webdevtutor
webdevtutor.net โ€บ blog โ€บ javascript-backslash-replace
Escape the Backslash in JavaScript: A Guide to Replacement Techniques
As you can see, the backslash (\) has escaped itself, resulting in an invalid JavaScript string. One way to replace backslashes is by using string methods like replace() or split().
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ javascript-replacing-the-escape-character-in-a-string-literal
How to Replace Backslash (Escape Character) in JavaScript String Literal: Solving the 'No Result' Issue โ€” javaspring.net
To replace all literal backslashes in a string, use a regular expression with the global (g) flag. The string search method ('\\') only replaces the first occurrence. For global replacement, regex is required.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Removing backslash out of my string - JavaScript - The freeCodeCamp Forum
July 20, 2018 - I am doing the Title Case a String challenge, and my final code is outputting (I canโ€™t get the backslash to show, but itโ€™s in the word โ€œIโ€™m) โ€˜Iโ€™m a little tea potโ€™, which wonโ€™t pass the test due to the slash. Iโ€™ve tried โ€ฆ
๐ŸŒ
JavaScripter
javascripter.net โ€บ faq โ€บ backslashinregularexpressions.htm
Backslash in JavaScript Regular Expressions
Backslash in Regular Expressions JavaScript FAQ | JavaScript Strings and RegExp FAQ ยท Question: How do I match a backslash using regular expressions
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-remove-backslash-from-json-string-in-javascript
How to Remove Backslash from JSON String in JavaScript? - GeeksforGeeks
July 23, 2025 - ... In this approach, we are using the replace() method with a regular expression (/\\/g) to globally match and remove all backslashes (\\) in the JSON string jStr, resulting in a cleaned JSON string res without backslashes.