-2

enter image description here My string is tel:\\99999999999. How i can replace '\' to '\' single? I want output like: tel:\99999999999.

Please see attached image backslashes not showing into question.

guradio
  • 15,524
  • 4
  • 36
  • 57

4 Answers4

3

You should escape the slash twice:

"tel:\\99999999999".replace('\\\\', '\\');
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
0

use .replace() to achieve what you want, str.replace("\\\\","\\")

var str = "tel:\\99999999999";

console.log(str.replace("\\\\","\\"))
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
  • If you `console.log(tel)` before the replace it still works – Washington Guedes Jun 28 '17 at 10:48
  • 1
    This answer has already been posted, and why do you include jquery?? – Denny Jun 28 '17 at 10:48
  • @Denny When I started to write the answer it was not posted, so that's why I made it, And the reason I included jquery is that it started with a jquery tag, so that's why. And if you look then I wrote the answer first in the comment, then I wanted to include it as an actual Answer. Please check the facts before – Carsten Løvbo Andersen Jun 28 '17 at 10:49
0

You can simply use tel.replace(/\\\\/g, "\\").

Demo:

var tel ="\\99999999999";

console.log(tel.replace(/\\\\/g, "\\"));

You need to escape the \ character with another \, because it's an escape character in JavaScript, you can check JavaScript backslash (\) in variables is causing an error for further reading.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
-2

You can do like this

"tel:\\99999999999".replace('\\\\', '');
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62