![](/img/trans.png)
[英]How to escape apostrophe or quotes on a JSP (used by JavaScript)
[英]How to Escape Single Quotes in Python on Server to be used in Javascript on Client
>>> sample = "hello'world"
>>> print sample
hello'world
>>> print sample.replace("'","\'")
hello'world
在我的網絡應用程序中,我需要存儲我的python字符串,所有單引號都被轉義,以便稍后在客戶端瀏覽器javascript中進行操作。 麻煩是python使用相同的反斜杠轉義表示法,因此上面詳述的替換操作無效。
希望有一個簡單的解決方法?
作為將數據從Python傳遞到Javascript的一般解決方案,請考慮使用json
庫(Python 2.6+中的標准庫的一部分)對其進行序列化。
>>> sample = "hello'world"
>>> import json
>>> print json.dumps(sample)
"hello\'world"
使用:
sample.replace("'", r"\'")
要么
sample.replace("'", "\\'")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.