我想做这样的事情:
<script>
var text = "this is some sample text that i want to replace";
var new_text = text.replace(/'/g, "\\'");
var new_text = text.replace(/"/g, "\"");
document.write(new_text);
</script>
但只要我的 var 中有一个 " ,text var text = "this is some sample "text" that I want to replace";
它就会破坏我的 javascript ,因此我无法替换它。
我的 var 文本来自 modx CMS tiny mce 字段,因此它是一种像这样显示的 $ [[*content]]。
这是我的 modx 模板代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>[[*titrepage]]</title>
<style type="text/css">
div.code_view {
height:auto;
}
div.code_demo {
/* margin:1em auto 1.4em; */
border:1px solid #ddd;
width: 535px;
color: #112433;
}
div#content div.code_demo ul.demoLinks {
margin:6px 6px 10px;
}
div#infoDiv {
padding:0 8px 8px;
}
div#infoDiv h4 {
margin:4px 0;
font-size:1.1em;
}
</style>
<script src="http://aurorafilms.eskem-studio.com/assets/templates/aurora/js/dw_contentswap.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">
function init_ContentSwap() {
var swap1 = {
restoreDefault: false,
content: {
'old': "<h4>Synopsis</h4><p>[[*synopsis]]</p>",
'infos': '<h4>Infos</h4><p>[[*infos]]</p>',
'presse': "<h4>Presse</h4><p>[[*Presse:jsonEncode]]</p>",
'festivals': "<h4>Festivals</h4><p>[[*festival]]</p>"
}
}
dw_ContentSwap.setup(swap1);
}
dw_Event.add(window, 'load', init_ContentSwap);
</script></head>
<body>
<div id="page"> <ul class="demoLinks">
<li style="z-index: 10; width: 80px; position: relative; "><a href="#old" class="showInfo old">Synopsis</a></li>
<li style="z-index: 9; width: 80px; position: relative; left: -20px;"><a href="#infos" class="showInfo infos">Infos</a></li>
<li style="z-index: 8; width: 80px; position: relative; left: -40px;"><a href="#presse" class="showInfo presse">Presse</a></li>
<li style="z-index: 7; width: 80px; position: relative; left: -60px;"><a href="#festivals" class="showInfo festivals">Festivals</a></li>
</ul><div id="content" style="float: left;" >
<div id="text">
<!-- demo -->
<div class="code_demo">
<div id="infoDiv"><h4>Synopsis</h4><p>[[*synopsis]]</p></div>
</div>
<!-- end demo -->
<p class="clearer"> </p></div> </div> </div>
</body>
</html>
那么最好的方法是什么?