45

I have this var

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

Which is

<div class=\"abcdef\">

But I need

<div class="abcdef">

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

8 Answers 8

65

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.

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

Comments

14

You can use JSON.parse to unescape slashes:

function unescapeSlashes(str) {
  // add another escaped slash if the string ends with an odd
  // number of escaped slashes which will crash JSON.parse
  let parsedStr = str.replace(/(^|[^\\])(\\\\)*\\$/, "$&\\");

  // escape unescaped double quotes to prevent error with
  // added double quotes in json string
  parsedStr = parsedStr.replace(/(^|[^\\])((\\\\)*")/g, "$1\\$2");

  try {
    parsedStr = JSON.parse(`"${parsedStr}"`);
  } catch(e) {
    return str;
  }
  return parsedStr ;
}

6 Comments

For me, with the slashes arising from JSON.stringify(), JSON.parse worked perfectly. Didn't need the str.replace(...) code
The str.replace is only needed if the string has an unescaped trailing slash, which will crash JSON.parse.
This will fail with { or } in the string
@ThomasWilliams seems to work fine with { or } for me jsfiddle.net/UziTech/npq5ba2L
Ah I think the issue is the \" your string is not double escaped so it returns the original string like it should.
|
7

If you want to remove backslash escapes, but keep escaped backslashes, here's what you can do:

"a\\b\\\\c\\\\\\\\\\d".replace(/(?:\\(.))/g, '$1');

Results in: ab\c\\d.

Explanation of replace(/(?:\\(.))/g, '$1'):

/(?:\\) is a non-capturing group to capture the leading backslash

/(.) is a capturing group to capture what's following the backslash

/g global matching: Find all matches, not just the first.

$1 is referencing the content of the first capturing group (what's following the backslash).

1 Comment

I would advise to remove the "global" flag though, as "a\\b\\\\c\\\\\\\\\\d".replace(/(?:\\(.))/, '$1'); results in: ab\\c\\\\\d.
1

Try this:

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

1 Comment

This will remove all backslashes, the question is how to remove backslash escaping.
0
var x = "<div class=\\\"abcdef\\\">";
alert(x.replace(/\\/gi, ''));

2 Comments

The i modifier is not needed here.
Neither is the alert or the definition of x
-1
'<div class=\\\"abcdef\\\">'.replace(/\\\"/g, '"')

Other answers have you delete all backslashes, you only want to delete the ones befoew the quotes.

Comments

-3

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

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

Comments

-5

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.

2 Comments

Using eval is a bad practice; unless you've carefully sanitized the data this could allow malicious code to execute.
but the way to unescape \056\045 like string

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.