Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I remove the (//) blocks; tags inside a script element in Javascript?
CDATA, the short form of Character Data, is a block of code that prevents parsing by the XML/HTML parser. Sometimes our data includes predefined characters like "<" and "&" which are not meant to be parsed, so we wrap them inside CDATA blocks.
To remove CDATA blocks from script tags using JavaScript, we have the following approaches:
- Using Regular Expression (Regex)
- Using the replaceAll() Method
Using Regular Expression (Complete Removal)
Regular expressions allow us to find and remove entire CDATA blocks including their content. This is the recommended approach when you want to completely remove CDATA sections.
Regex Pattern
/<!\[CDATA\[[\s\S]*?\]\]>/g
Syntax
string.replace(regex, "")
Here string is the text containing CDATA blocks, and regex is the regular expression pattern to match CDATA sections.
Example
In this example, we remove complete CDATA blocks including their content using the replace() method with regex:
<html>
<body>
<h2>Removing CDATA Blocks with Regex</h2>
<div id="output"></div>
<script>
let scriptContent = `<script type="text/javascript">
<![CDATA[
var A = new Array();
console.log("Inside CDATA block");
// Some JavaScript code here
]]>
</script>
Some other content...
<script type="text/javascript">
<![CDATA[
var B = new Array();
alert("Another CDATA block");
]]>
</script>`;
// Remove CDATA blocks completely
let cleanedContent = scriptContent.replace(/<!\[CDATA\[[\s\S]*?\]\]>/g, "");
document.getElementById("output").innerHTML = "<pre>" + cleanedContent + "</pre>";
</script>
</body>
</html>
Using replaceAll() Method (Tags Only)
The replaceAll() method can remove only the CDATA tags (<![CDATA[ and ]]>) while preserving the content inside. This approach is useful when you want to keep the JavaScript code but remove the CDATA wrapper.
Syntax
string.replaceAll("<![CDATA[", "").replaceAll("]]>", "")
Example
In this example, we remove only the CDATA tags while keeping the JavaScript content:
<html>
<body>
<h2>Removing Only CDATA Tags</h2>
<div id="output2"></div>
<script>
let scriptContent = `<script type="text/javascript">
<![CDATA[
var A = new Array();
console.log("This content will be preserved");
]]>
</script>
<script type="text/javascript">
<![CDATA[
var B = new Array();
console.log("This content will also be preserved");
]]>
</script>`;
// Remove only CDATA tags, keep content
let result = scriptContent.replaceAll("<![CDATA[", "")
.replaceAll("]]>", "");
document.getElementById("output2").innerHTML = "<pre>" + result + "</pre>";
</script>
</body>
</html>
Comparison
| Method | Removes CDATA Tags | Removes CDATA Content | Use Case |
|---|---|---|---|
| Regex replace() | Yes | Yes | Complete CDATA removal |
| replaceAll() | Yes | No | Keep content, remove tags only |
Conclusion
Use regex with replace() for complete CDATA removal, or replaceAll() when you need to preserve the JavaScript content inside CDATA blocks. The regex approach is more comprehensive and handles complex CDATA structures better.
