This question already has answers here:
How to escape apostrophe or quotes on a JSP (used by JavaScript)

(8个答案)


已关闭8年。




JSP:
<% final String data = "some test with ' single quotes"; %>
<script>
    var str = '<%= data %>';
<script>

结果是(JavaScript):
var str = 'some test with ' single quotes';



如何用\'替换单引号以避免JavaScript错误?

最佳答案

使用Apache Commons Lang软件包中的 escapeEcmaScript 方法:



例子:

关于java - java-字符串中的单引号转义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15575299/

10-11 05:49