0

I have a string like this coming from the server and its not working due to four backslashes. if i remove four with two its working.

URL_https~~\\\\fbcdn-sphotos-f-a.akamaihd.net\

May I know how to replace four backslashes with two as below

URL_https~~\\fbcdn-sphotos-f-a.akamaihd.net\

I tried various things but nothing worked out

i tried as follows

one:

strTest2.replace("\\\\\\\\","\\\\"

two:

strTest2 .replace(/[/\*]/, "");

Three:

strTest2.replace(/\|\|/g, "\\");
kobe
  • 15,671
  • 15
  • 64
  • 91
  • Sounds like you have a server problem that should be solved on the serverside, not with javascript ? – adeneo Aug 22 '13 at 18:03

2 Answers2

4

You need to store the new string created

strTest2 = strTest2.replace("\\\\\\\\","\\\\");

all of the replace methods return a new string. not alter the current string.

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
3

You need to assign the result, because strings are immutable.

The first one would have actually worked, but it only replaces the first occurrence of four backslashes. To replace all occurrences, you need to use an actual regex literal:

strTest2 = strTest2.replace(/\\\\\\\\/g,"\\\\");

You can improve readability of the above expression with a quantifier:

strTest2 = strTest2.replace(/(?:\\){4}/g,"\\\\");
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • @kobe actually, my reasoning was wrong, because `replace` doesn't even interpret strings as regex patterns. The actual catch was the assignment. The regex is important, though, if you want to replace more than one occurrence of four backslashes. – Martin Ender Aug 22 '13 at 18:11