-2

I have seen a lot of duplicates but none of them works

I spend a day to get this thing up. Gone through all duplicates

I need to replace a single backslash with a double backslash.

I am able to replace all other characters with a double backslash.

console.log("hello & 100\|".replace(/([&%$#_{}])/g, "\\"));

I know that two backslashes is indeed used to make a single backslash.

I tried all the possible ways of using regex as well. But neither works.

Here is another snippet I am using.

var replaceableString = "A\B";
replaceableString = replaceableString.replace(/\\/g, "\\\\");
console.log(replaceableString);

Unfortunately, this also is not working.

Please share if there are any workarounds.

SFKID
  • 59
  • 9
  • For `var replaceableString = "A\B"` if you log in console it will display `"AB"`. You should be using `var replaceableString = "A\\B"` which will be `"A\B"` then your `replaceableString.replace(/\\/g, "\\\\");` will work perfectly and you can see `A\\B` in console. – Karan Aug 26 '20 at 06:35
  • @Karan but how to replace it programmatically? What if the user enter single backslash? – SFKID Aug 26 '20 at 06:36
  • In code `"\\"` is a single backslash and `"\\\\"` is a double backslash. It has to be escaped between `"`-s, and the escape character is a backshlash. Consequentally `"A\B"` is an `A` and an escaped `B`. – tevemadar Aug 26 '20 at 06:40
  • @tevemadar So you are telling that we cant replace single backslash with two backslash as it is an escape character – SFKID Aug 26 '20 at 06:46
  • However, if it is an [XYProblem](https://en.m.wikipedia.org/wiki/XY_problem), and you want to create printable code string from control characters, then it's not the backslash you can look for, but the entire character. Like `"Hello\n"` contains no backslash, it is a 6 character long string, you can check, where the last character is a line-break, `"\n"`. That's a single character in the string regardless of being two in source code. So this kind of task requires separate handling of `\n`, `\t`, etc. and replace them with `\\n`, `\\t`. – tevemadar Aug 26 '20 at 06:50
  • As you said So this kind of task requires separate handling of \n, \t, etc. and replace them with \\n, \\t. How can I solve this – SFKID Aug 26 '20 at 06:57
  • Does this answer your question? [Javascript - How to show escape characters in a string?](https://stackoverflow.com/questions/21672334/javascript-how-to-show-escape-characters-in-a-string) – tevemadar Aug 26 '20 at 07:25
  • Apparently there is a workaround as the JSON encoder can do it, see proposed duplicate. – tevemadar Aug 26 '20 at 07:26
  • Please refer this, hope you would get better idea https://www.w3schools.com/js/js_strings.asp#:~:text=Escape%20Character&text=from%20the%20north.-,%22%3B,Code. – Karan Aug 26 '20 at 07:29
  • @tevemadar "\"AB\"" This is the output i am getting when i tried to stringyfy – SFKID Aug 26 '20 at 07:31
  • 1
    That's normal, `"\B"` is not a valid escape sequence (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation). The JS engine just throws it away. Please run these lines in the JS console: `console.log("A\B","A\B".length);` and `console.log("A\\B","A\\B".length);` if you haven't done such checks so far. – tevemadar Aug 26 '20 at 08:02
  • Thanks for helping. Dont know why it is getting down voted – SFKID Aug 26 '20 at 08:59

1 Answers1

0
var str = "AAA\\BBB\\CCC\\DDD\\EEE";
var rgx = /\\/g;
var res = str.replace(rgx, "\\\\");
console.log(res);

Comments below questions are very revealing. Single backslash \ is escape character. To obtain single backslash we must use double backslashes. So we must use four backslashes(in reality double) for replace the backslash.

Muzaffer Galata
  • 580
  • 1
  • 9
  • 22