简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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