简体   繁体   English

javascript转义字符串

[英]javascript escape string

I have the following string: 我有以下字符串:

'You've just created' “您刚刚创建的”

When I assign this string to a JavaScript variable, this is interpreted as 2 strings because there's a ' character. 当我将此字符串分配给JavaScript变量时,由于存在一个'字符,因此将其解释为2个字符串。

How can I escape it? 我该如何逃脱呢?

In JS code blocks, use a backslash: 在JS代码块中,使用反斜杠:

var text = 'You \'ve just created'

in JS inside HTML, use a HTML entity: 在HTML内的JS中,使用HTML实体:

<a onclick='alert("You &apos;ve just created");'>

You can use either " or ' to quote a string, so you could do it this way: 您可以使用“或'来引用字符串,因此可以这样进行:

"You've just created"

or you can use a \\ to quote just one character: 或者,您也可以使用\\引用一个字符:

'You\'ve just created'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM