JavaScript删除特殊字符串不起作用 [英] JavaScript Remove special characters string not working

查看:85
本文介绍了JavaScript删除特殊字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除可能出现在我的Google Analytics代码中的特殊字符,因为特殊字符似乎在某些版本的IE中导致脚本错误。我有这个功能:

I'm trying to remove special characters that could appear within my Google Analytics tags, as the special characters seem to be causing script errors in some versions of IE. I have this function:

                function removeSplChars(inStr) {
                inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
                return inStr;
                }

并且目前有GA代码可用:

and there is the GA code that currently works:

                <script type="text/javascript">
                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>
                 _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>

但是当我在代码中放置该功能时,我仍然会看到Chrome调试器中出现特殊字符。例如,当我拉出一个名为Matt's的产品页面时,它显示为Matt's。我想要的是马特。我们有其他产品名称,带有&符号和其他特殊字符,所以我只想允许A-z和数字(大写/没有上限可以)

But when I place that function within the code, I still see special characters coming up in the Chrome Debugger. For example, when I pull up a page that has a product called "Matt's" it shows up as Matt's. What I want is Matts. We have other product names with ampersands and other special characters, so I just want to allow A-z and numbers (caps/no caps are OK)

任何建议都将不胜感激。我已经在SO上查看了以下帖子,但到目前为止还没有找到任何可以帮助我完成这项工作的帖子:

Any advice would be appreciated. I have looked at the following posts on SO but so far not found anything that helps me make this work:

如何在javascript中处理(®'¿¡¡À)特殊字符?

javascript regexp删除所有特殊字符

使用JavaScript删除除字符串空格外的所有特殊字符

我是JSP和JavaScript的新手,所以我确信我没有将代码放在正确的位置,或者我可能需要在页面上添加其他内容?我尝试将removeSplChars函数放在()中或添加;没有运气不幸的是,我必须在工作中学习这一点,所以我必须专注于完成我给予的任务,而不是花时间真正理解语言的逻辑/语法。

I am new to JSP and JavaScript so I am sure I'm not placing the code in the right place, or maybe I need to add something else on the page? I have tried placing the removeSplChars function within () or adding a ; and no luck. Unfortunately, I have to learn this on the job so I have to focus on completing the tasks I've been given rather than taking the time to truly understand the logic/syntax of the language.

推荐答案

问题不在于函数本身,它来自其他地方。证明: http://jsfiddle.net/wDaCw/

The problem is not the function itself, it comes from somewhere else. Proof: http://jsfiddle.net/wDaCw/

这篇关于JavaScript删除特殊字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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