最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jul 13, 2020 at 10:34 0Valt 10.3k9 gold badges39 silver badges64 bronze badges asked Jul 8, 2011 at 18:17 swickbladeswickblade 4,6565 gold badges22 silver badges24 bronze badges 1
  • Well, i think you need to split it in anyway you could try something like this: var myString = "She's " + '"amazing!"'. does it helps? – Hector Sanchez Commented Jul 8, 2011 at 18:21
Add a comment  | 

5 Answers 5

Reset to default 13

You need to escape the quotation characters with \:

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

Is it possible, in javascript, to declare a string with information inside of it, that does not use single or double quotes?

No. JavaScript string literals are delimited with either single quotes or double quotes. Those are the only choices.

does anyone know a simple and easy way to escape these quotes so I can put it in a string?

Do you mean in a string literal?

var str = "var foo=\"bar\"; function say(it) { alert('It is: ' + it); say(foo);";

Or programmatically?

str = str.replace(/"/g, '\\"');

An easy way to escape the quotes is to use the javascript escape function.

http://www.w3schools.com/jsref/jsref_escape.asp

Use the escape function to replace special characters (including single and double quotes). You can then use the unescape function to return the string to it's normal state later if necessary.

For example:

var data = 'hello my name is "James"';
alert(escape(data)); //Outputs: hello%20my%20name%20is%20%22James%22

I know that this is an old question but there is a way of handling it that no one here brought up yet and it's template literals. Basically, this symbol (`), known as backticks. They work the same way as single and double quotes, with the addition of multi-line support.

For example, the below String contains both quote types. Using backticks, this fixes the problem:

var str = `This is a quote ' and a doublequote ".`;

Since backticks aren't commonly used (or found on the keyboard), it shouldn't cause any issues.

EDIT: Nvm. Backticks are on the keyboard. Still not very commonly used though.

发布评论

评论列表(0)

  1. 暂无评论