如何在客户端的JavaScript中使用Python中的单引号 [英] How to Escape Single Quotes in Python on Server to be used in Javascript on Client

查看:109
本文介绍了如何在客户端的JavaScript中使用Python中的单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> sample = "hello'world"
>>> print sample
hello'world
>>> print sample.replace("'","\'")
hello'world

在我的网络应用程序中,我需要存储我的python字符串与所有单引号转义,以便稍后在客户端浏览器javascript。麻烦是python使用相同的反斜杠逃逸符号,所以上面所述的替换操作没有任何效果。

In my web app I need to store my python string with all single quotes escaped for manipulation later in the client browsers javascript. Trouble is python uses the same backslash escape notation so the replace operation as detailed above has no effect.

希望有一个简单的解决方法?

Hopefully there is a simple workaround?

推荐答案

使用:

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

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

这篇关于如何在客户端的JavaScript中使用Python中的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆