Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript Remove special characters string not working
    text
    copied!<p>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:</p> <pre><code> function removeSplChars(inStr) { inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, ""); return inStr; } </code></pre> <p>and there is the GA code that currently works:</p> <pre><code> &lt;script type="text/javascript"&gt; var _gaq = _gaq || []; _gaq.push(['_setAccount', '&lt;c:out value="${profileId}"/&gt;']); &lt;c:choose&gt; &lt;c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" &gt; _gaq.push(['_setCustomVar', 2, // This custom var is set to slot #2. '&lt;c:choose&gt;&lt;c:when test="${WCParam.source eq 'search'}"&gt;Search&lt;/c:when&gt;&lt;c:otherwise&gt;&lt;c:out value="${topCat}" /&gt;&lt;/c:otherwise&gt;&lt;/c:choose&gt;', // The top-level name for your online content categories. '&lt;c:choose&gt;&lt;c:when test="${WCParam.source eq 'search'}"&gt;Search &lt;c:out value="${WCParam.searchTerm}" /&gt;&lt;/c:when&gt;&lt;c:otherwise&gt;&lt;c:out value="${topCat}" /&gt;|&lt;c:out value="${subCatA}" /&gt;|&lt;c:out value="${subCatB}" /&gt;|&lt;c:out value="${subCatC}" /&gt;&lt;/c:otherwise&gt;&lt;/c:choose&gt;', // Records value of breadcrumb name 3 // Sets the scope to page-level. ]); &lt;/c:when&gt; &lt;c:otherwise&gt; &lt;/c:otherwise&gt; &lt;/c:choose&gt; _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); })(); &lt;/script&gt; </code></pre> <p>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&#039;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)</p> <p>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:</p> <p><a href="https://stackoverflow.com/questions/3004487/how-to-handle-special-characters-in-javascript">How to handle (&#174; &#180; &#169; &#191; &#161; &#176; &#192; ) special characters in javascript?</a><br/> <a href="https://stackoverflow.com/questions/4374822/javascript-regexp-remove-all-special-characters">javascript regexp remove all special characters</a><br/> <a href="https://stackoverflow.com/questions/6555182/remove-all-special-characters-except-space-from-a-string-using-javascript">Remove all special characters except space from a string using JavaScript</a></p> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload