开发者

javascript escape string

开发者 https://www.devze.com 2023-01-22 14:33 出处:网络
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 \' charact开发者_开发问答er.

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 ' charact开发者_开发问答er.

How can I escape it?


In JS code blocks, use a backslash:

var text = 'You \'ve just created'

in JS inside HTML, use a HTML entity:

<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'
0

精彩评论

暂无评论...
验证码 换一张
取 消