0

I'm trying to remove the CDATA wrapper in the following script (content has to be wrapped by CDATA to pass XHTML validation):

<script id="tplTest" type="text/html">

//<![CDATA[ 
<p id="msg">Hello</p>
<p>Another test: <#= ddd.ArtID #></p> 
//]]>

</script> 

JavaScript:

var strTmp = document.getElementById("tplTest").innerHTML;
var strNew = strTmp.replace(/[\/(\/!\[)\]CDATA]/g, "").replace(/[(\/\/\]\])]/g, "");

It removes most to the CDATA mark-up except for the start/end (<, >) tags:

< 
<p id="msg">Hello<p>
<p>nother test: <#= ddd.rtI #><p> 
>

Question: How should I modify the regex to additionally remove these leading and trailing <, > tags?

2 Answers 2

5

You could just replace the raw string and skip using regular expressions all-together:

"FOO BAR".replace("FOO", ""); // replace "FOO" with "" (nothing)

In your case:

var stringToSanitize = "//<![CDATA[ xxx //]]>";

var sanitizedString = stringToSanitize
                      .replace("//<![CDATA[", "")
                      .replace("//]]>", "");

Regular expressions in JavaScript are slow. So on top of you getting your problem solved, you might see a slight speed-increase using my example.

Sign up to request clarification or add additional context in comments.

Comments

0

Isn't it enough to just add a < after the first slash in the first replace and a '>' after the last slash in the last replace? If your regex dialect takes these angle brackets as magic characters (few do) you can use \< and \> respectively, i.e., escape them with backslashes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.