How to convert literal strings to their escape sequence equivalents? [duplicate]

ساخت وبلاگ

Vote count: 15

I have this var

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

Which is

<div class="abcdef">

But I need

<div class="abcdef">

How can I "" this var to remove all escaping characters?

asked Jul 10 '11 at 9:50

5 Answers

Vote count: 22 accepted

You can replace a backslash followed by a quote with just a quote via a regular expression and the String#replace function:

var x = "<div class=\"abcdef\">";
x = x.replace(/\"/g, '"');
document.body.appendChild( document.createTextNode("After: " + x)
);

Note that the regex just looks for one backslash; there are two in the literal because you have to escape backslashes in regular expression literals with a backslash (just like in a string literal).

The g at the end of the regex tells replace to work throughout the string ("global"); otherwise, it would replace only the first match.

answered Jul 10 '11 at 9:53

Vote count: 10

Try this:

x = x.replace(/\/g, "");
answered Jul 10 '11 at 9:53

Vote count: 1

var x = "<div class=\"abcdef\">";
alert(x.replace(/\/gi, ''));
answered Jul 10 '11 at 9:54

Vote count: -1

Let me propose this variant:

function un(v) { eval('v = "'+v+'"'); return v; }

This function will not simply remove slashes. Text compiles as code, and in case correct input, you get right unescaping result for any escape sequence.

answered Feb 11 '13 at 21:53

Vote count: -1

You need to make there be one backslash instead of three.
Like this:

var x = "<div class="abcdef">"; 
answered Dec 29 '13 at 14:11

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : how to convert pdf to word,how to convert pdf to jpg,how to convert degrees to radians,how to convert grams to moles,how to convert c to f,how to convert a fraction to a decimal,how to convert pdf to excel,how to convert binary to decimal,how to convert mp4 to mp3,how to convert cm to m, نویسنده : استخدام کار backsoft بازدید : 432 تاريخ : يکشنبه 16 آبان 1395 ساعت: 6:19