1

I am currently working on a project where I don't have an access to their server side. All I can do is retrieve the blog data from their template editor, but it seems like there's no way to escape HTML using their template syntax. This is bad, but I need to assign the HTML in a javascript variable to create a Json data.

In Python, you'd use both apostrophes and quotation marks without escaping it by using three apostrophes as such:

example = ''' 
            this '/ is great! ""Wahjhahaha
          '''

Is there any equivalent way of this in Javascript? As I mentioned, I can't escape the HTML data.

Yongju Lee
  • 241
  • 4
  • 5
  • 1
    If browser compatibility isn't an issue then you can use backtip `\`` but it's definitely and undoubtedly a huge security risk. If you can get it as JSON... – Adriano Repetti May 10 '18 at 06:23
  • Hello Mamun! Thanks for the reply. The example is Python. I see it has to be '/, but I am giving you an example that it works without escaping it in Python. Thanks! – Yongju Lee May 10 '18 at 06:24
  • @AdrianoRepetti Browser compatibility is **never** an issue. Use a transpiler and stop caring... – baao May 10 '18 at 06:24
  • 1
    The escape character in javascript is a `\\`, not a `/` - and it has to be before the to be escaped character, obviously – baao May 10 '18 at 06:24
  • @baao if OP is using some weird home-made template engine (I can't imagine any other reason for this issue) then transpilers might not be an option – Adriano Repetti May 10 '18 at 06:27
  • As @AdrianoRepetti mentioned it use backtip, i.e `var stringToScape = \`this '/ is great! ""Wahjhahaha\`;` – ricardoorellana May 10 '18 at 06:27
  • It's called "backtick". – connexo May 10 '18 at 06:30
  • The backtip trick seems to do the trick! Thanks, but I may need to investigate further as IE (even the version 11) doesn't seem to support this. – Yongju Lee May 10 '18 at 06:31
  • https://stackoverflow.com/questions/2195568/how-do-i-add-slashes-to-a-string-in-javascript – Krzysztof Janiszewski May 10 '18 at 08:43

2 Answers2

1

"\" is the escape character in javascript.

var example = "This is a string with \' \" ";
console.log(example);
Ramesh
  • 13,043
  • 3
  • 52
  • 88
0

There are several approches depending on what you want to do.

You can use single quoute inside double quote.

You can use double quote inside single quote.

And if you want to use double quote inside double quote you have to use backslash (\) before the element. The same applies for single quote

console.log('This is double quote inside single quote "');
console.log("This is single quote inside double quote '");
console.log('This is single quote inside single quote \'');
console.log("This is double quote inside double quote \"");
console.log('\' " " \'');
console.log("' \" \" '");

EDIT

You need to use function replace()

var str = "Using single quotes ' ' sdsd 'sd 'dssd sdf' 'sdf'";
str = str.replace(/'/g, "\\'");
console.log(str);

Similarly you can do with double quotes.

Alternatively you can use backticks, but I would not recommend it (backticks in IE11)

console.log(`Here you can use both ' single and " double quotes without backslashes`);
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38