How to remove backslash escaping from a javascript var? -


i have var

var x = "<div class=\\\"abcdef\\\">"; 

which is

<div class=\"abcdef\"> 

but need

<div class="abcdef"> 

how can "unescape" var remove escaping characters?

you can replace backslash followed quote quote via regular expression , string#replace function:

var x = "<div class=\\\"abcdef\\\">";  x = x.replace(/\\"/g, '"');  document.body.appendchild(    document.createtextnode("after: " + x)  );

note regex looks 1 backslash; there 2 in literal because have escape backslashes in regular expression literals backslash (just in string literal).

the g @ end of regex tells replace work throughout string ("global"); otherwise, replace first match.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

Voice chat over bluetooth between Android & Python (pybluez) -

Integrate jquery-globalize into an ASP.NET Core MVC App -