开发者

Escape characters to be used in a javascript literal string

开发者 https://www.devze.com 2023-03-17 09:44 出处:网络
I would like to t开发者_Python百科ake a string that the user will enter to a text box in a form and turn it into a javascript literal. So I\'d, for instance, turn the \" character into \\\".

I would like to t开发者_Python百科ake a string that the user will enter to a text box in a form and turn it into a javascript literal. So I'd, for instance, turn the " character into \".

Is there any complete list of characters that would need to be escaped?


You only need to escape your delimiter and/or whatever enclosing quotes you choose (escape double quotes if you enclose your string in double, escape single otherwise).

If you're going to render it as HTML, you may wnat to convert them to entities (e.g. & -> &)

0

精彩评论

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