0

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.

1

2 Answers 2

2
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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you TylerAndFriend, but i've already tried your suggest and it doens't work. I've found the solution, i don't know why but the pattern need \ 3 times var a = "\\{0\\}"; var b = a.replace(/\\\/g, ''); console.log(b) will print {0}
@user2548436 The above answer, with a pattern of /\\/g, worked for me. In Chrome console, the statement "\\{0\\}".replace(/\\/g, '') yields "{0}". Using a pattern of /\\\/g gives a SyntaxError.
1

Try this

var a = "\\{0\\}";
var b = a.replace('\\','').replace('\\','');
alert(b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.