JavaScript Remove special characters string not wo

2020-02-16 04:16发布

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;
                }

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>

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)

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:

How to handle (® ´ © ¿ ¡ ° À ) special characters in javascript?
javascript regexp remove all special characters
Remove all special characters except space from a string using JavaScript

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.

2条回答
Melony?
2楼-- · 2020-02-16 04:41

In a comment I asked you to show us the code where your function removeSplChars was called, and to my surprise you told me that you didn't call it anywhere. Well, that answers the question why nothing has changed. If you don't call the function with the data you want to change, then nothing get changed.

I'm trying to understand what your problem is, because it shouldn't be a problem... Then I looked closer to your code and spotted this:

'<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>'

You should notice that the color coding has made the word search in another color than the rest of the string. This is because you have started the string with a single quote and therefore the string will end at the next single quote.

I notice that you are using JSP, which I'm not familiar with. I looked it up and saw that these tags are parsed by your server before they are sent to the client, and replaced with some text.

Since you have problems with special characters like a single quote, the problem is obvious: The string that your tags output isn't escaped to be a part of a javascript string. I think your only problematic characters of all possible Unicode characters is a double quote (u+0022), single quote (u+0027) and backslash (u+005c), because they must be escaped with a backslash (like \", \' and \\)

Now, how do you do that in JSP? I don't know, but I know that you aren't the first one to have this problem. I quick search gave me this page where they suggest the following:

<spring:message code="${escapedString}" javaScriptEscape="true"/>

Now, as I said, I'm not familiar with JSP, so I am only guessing. Try to replace all your uses of <c:out value="xxx"/> like this:

<c:out value="${WCParam.searchTerm}" />

with this:

<spring:message code="${WCParam.searchTerm}" javaScriptEscape="true"/>

If that doesn't work look at this answer.

查看更多
Explosion°爆炸
3楼-- · 2020-02-16 04:47

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

查看更多
登录 后发表回答