How to escape single quotes in Python on a server to be used in JavaScript on a client

谁说胖子不能爱 提交于 2019-11-30 05:52:15

Use:

sample.replace("'", r"\'")

or

sample.replace("'", "\\'")

As a general solution for passing data from Python to Javascript, consider serializing it with the json library (part of the standard library in Python 2.6+).

>>> sample = "hello'world"
>>> import json
>>> print json.dumps(sample)
"hello\'world"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!