Search code examples
javascriptreplacespecial-characters

javascript replace \ character


i've a pure javascript var containing the following value:

    var a = "\\{0\\}";

now what i need to do is replace the two \ before the first and latter bracket.

I've tried different solution, but no one seems to work

   var b = a.replace(pattern,'')

I can't find a good pattern. I don't know what to do or how to write a good pattern or a regexpr.

Anyone know the solutions? Thank to all

P.S. I need the solution in pure javascript.


Solution

  • var a = "\\{0\\}";
    var pattern = /\\/g;
    var b = a.replace(pattern,'');
    

    Your problem is that you're dealing with escape characters, so you needed to use a regex.


    edit: working example

    http://jsfiddle.net/tylerpachal/HgBnw/