可能重复:
JavaScript Remove special characters string not working
我正在尝试删除发送到Google Analytics的动态填充值中的特殊字符; '像Matt这样的产品'会导致JS错误。
这对我来说尤其具有挑战性,因为我并不真正了解JavaScript或JSP。我编写了以下代码,但它没有达到预期的效果。还有另一种方法吗?我必须直接在锚标签中修改自定义变量和_trackEvent调用。以下是自定义变量的代码:
<script type="text/javascript">
function removeSplChars(inStr) {
inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
return inStr;
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<c:out value="${profileId}"/>']);
<c:choose>
<c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" >
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #2.
'<c:choose><c:when test="${WCParam.source eq 'search'}">Search</c:when><c:otherwise><c:out value="${topCat}" /></c:otherwise></c:choose>', // The top-level name for your online content categories.
'<c:choose><c:when test="${WCParam.source eq 'search'}">Search <c:out value="${WCParam.searchTerm}" /></c:when><c:otherwise><c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" /></c:otherwise></c:choose>', // Records value of breadcrumb name
3 // Sets the scope to page-level.
]);
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
removeSplChars('<c:out value="${topCat}" />', '<c:out value="${subCatA}" />', '<c:out value="${subCatB}" />', '<c:out value="${subCatC}" />');
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
举一个例子,在用 //评论痕迹符号名称评论的行中,谷歌正在输出如下代码:
[2,厨房工具,厨房工具|纺织品|厨师&amp; #0 3 9; s围裙|,3]“(当然,符号之间没有空格)
我正在寻找的是什么
[2,厨房工具,厨房工具|纺织品|厨师围裙|,3]“
我尝试在语句中添加escapeXml =“false”,但这会给我一个“意外标识符”错误,但没有详细信息。
现在,我也尝试过以下代码。我希望它用test替换特殊字符,但没有任何反应。
<script type="text/javascript" src="<c:out value="${jspStoreImgDir}javascript/Util.js"/>">
String.prototype.unescapeHtml = function () {
var temp = document.createElement("li");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild);
return result;}
'<c:out value="${catNameDisplay}" />'.unescapeHtml().replace((/[^a-zA-Z0-9 ]/g, 'test');
</script>
非常感谢任何帮助。我一整天都在努力,找不到解决办法。
答案 0 :(得分:0)
抱歉,以前的回答很愚蠢。我道歉。应该完全相反。
我在这里注意到的一件事是你可能会将多个参数传递给removeSplChars
方法。函数的结构方式,它只会修复第一个字符串。
此外..正在返回'固定'字符串,而您假设它将被内联修复
你正在做类似的事情:removeSplChars(arg1, arg2, arg3, arg4);
并期望arg1
,arg2
,arg3
和arg4
是固定的,而他们却不是。更好的是:
arg1 = removeSplChars(arg1);
arg2 = removeSplChars(arg2);
arg3 = removeSplChars(arg3);
arg4 = removeSplChars(arg4);
好的,所以这'应该'解决你的问题。玩得开心!
答案 1 :(得分:0)
它可以像这样逃脱:
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${fn:escapeXml(myString)}
答案 2 :(得分:0)
String.prototype.unescapeHtml = function () {
var temp = document.createElement("div");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild);
return result;
}
'''.unescapeHtml().replace(/[^0-9a-zA-Z]/gi, '');
我从http://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/获得了unescapeHtml代码并应用了一个正则表达式来删除它后的特殊字符。
试一试。只需替换我为变量添加的字符串。