Regex remove escape characters javascript

I have this var

var x = "
";

Which is

But I need

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

asked Jul 10, 2011 at 9:50

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

var x = "
"; 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, 2011 at 9:53

Regex remove escape characters javascript

T.J. CrowderT.J. Crowder

982k181 gold badges1827 silver badges1800 bronze badges

0

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 ;
}

answered Feb 18, 2018 at 19:45

Tony BrixTony Brix

3,9057 gold badges39 silver badges50 bronze badges

6

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).

answered Aug 9, 2019 at 12:42

Regex remove escape characters javascript

BrixomaticBrixomatic

3414 silver badges14 bronze badges

1

Try this:

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

answered Jul 10, 2011 at 9:53

ElonU WebdevElonU Webdev

2,43313 silver badges15 bronze badges

1

var x = "
"; alert(x.replace(/\\/gi, ''));

answered Jul 10, 2011 at 9:54

Alex EmilovAlex Emilov

1,2133 gold badges18 silver badges25 bronze badges

2

'
'.replace(/\\\"/g, '"')

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

answered Sep 11, 2017 at 6:58

Aryeh ArmonAryeh Armon

2,1102 gold badges21 silver badges36 bronze badges

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

var x = "
";

answered Dec 29, 2013 at 14:11

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, 2013 at 21:53

2

How do I remove an escape character from a string?

JSON Escape / Unescape.
Backspace is replaced with \b..
Form feed is replaced with \f..
Newline is replaced with \n..
Carriage return is replaced with \r..
Tab is replaced with \t..
Double quote is replaced with \".
Backslash is replaced with \\.

How do you escape a backward slash in JavaScript?

The backslash() is an escape character in JavaScript. The backslash \ is reserved for use as an escape character in JavaScript. To escape the backslash in JavaScript use two backslashes.

How do you remove slashes from a string?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.

What are escape characters in regex?

The \ is known as the escape code, which restore the original literal meaning of the following character. Similarly, * , + , ? (occurrence indicators), ^ , $ (position anchors) have special meaning in regex. You need to use an escape code to match with these characters.