-1

I'm having issues with something pretty easy but I am wondering can anyone help me. I have an escape character in my Javascript object to escape an apostrophe, but this character is being lost once I assign it into the x variable. Does anyone know how I can keep this \ character in the name? Thanks

var x = { Name: 'Matt\'s Test'};
var y = x.Name;
console.log(y);
MattTheHack
  • 1,354
  • 7
  • 28
  • 48
  • 1
    You *don't* have an escape character. In the string literal `\'` is already an escape pattern, so you get an escaped `'` which has no effect (other than let you use the same for delimiters). To get an actual `\\` as string *content* you need to escape it in the string *literal*. – VLAZ Jan 29 '19 at 12:00

1 Answers1

0

If you want to render a backslash in a string you have to escape it as it is used as the escape character. Add another slash and escape it.

var x = { Name: 'Matt\\\'s Test'};
var y = x.Name;
console.log(y);
James Coyle
  • 9,922
  • 1
  • 40
  • 48