How can I accommodate a string with both single and double quotes inside of it in JavaScript

swickblade picture swickblade · Jul 8, 2011 · Viewed 11.1k times · Source

I have an application, and it is fed some HTML. It then needs to put that HTML into a string. This HTML contains single and double quotes. Is it possible, in javascript, to declare a string with information inside of it, that does not use single or double quotes?

I guess if it is not possible, does anyone know a simple and easy way to escape these quotes so I can put it in a string? Keep in mind, part of this string will be JavaScript that I will later need to execute.

Answer

digitalbath picture digitalbath · Jul 8, 2011

You need to escape the quotation characters with \:

var someString = 'escape all the quotation marks \"\'';