Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 273
swickblade Avatar asked Jul 08 '11 18:07

swickblade


People also ask

How do you insert a single or double quote character in a string?

To place quotation marks in a string in your code In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark.

How do you put quotes inside a string in JavaScript?

Enclosing Quotation Marks You'll need to use opposite quotation marks inside and outside of JavaScript single or double quotes. That means strings containing single quotes need to use double quotes and strings containing double quotes need to use single quotes.

How do you pass a string with double quotes?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character.

Can we use single quotes in place of double quotes to declare a string variable?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!


1 Answers

You need to escape the quotation characters with \:

var someString = 'escape all the quotation marks \"\'';
like image 123
digitalbath Avatar answered Sep 22 '22 09:09

digitalbath