• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

JavaScript: Replace all HTML tags with placeholders

Inside <div id="content"></div> I want to replace all opening tags with **= and I want to replace all closing tags with =**
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
</head>
<body>

<div id="content">
<h1 class="h">Heading</h1>
<p>Hello <strong class="w">World</strong>.</p>
<p><span style="color: red">red</span> <span style="color: blue">blue</span>  <span style="color: green">green</span></p>
</div>

<script type="text/javascript">
/*<![CDATA[*/

// Thanks, hielo

var obj=document.getElementById('content');
if(obj){
	var tagsOpen=obj.innerHTML.match(/(<[a-zA-Z]+[^>]*?>)/g);
	alert('Opening Tags: \n\n'+tagsOpen.join("\n"))

	var tagsOpen=obj.innerHTML.match(/(<\/[a-zA-Z]+[^>]*?>)/g);
	alert('Closing Tags: \n\n'+tagsOpen.join("\n"))

}

alert('
**=Heading=**
**=Hello **=World=**.=**
**=**=red=** **=blue=**  **=green=**=**
');

/*]]>*/
</script>

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
StealthyDevCommented:
Find below the script.

Hope it helps..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
</head>
<body>

<div id="content">
<h1 class="h">Heading</h1>
<p>Hello <strong class="w">World</strong>.</p>
<p><span style="color: red">red</span> <span style="color: blue">blue</span>  <span style="color: green">green</span></p>
</div>

<script type="text/javascript">
/*<![CDATA[*/

// Thanks, hielo

var obj=document.getElementById('content');
if(obj){
	var myInnerHTML = "";
	var tagsOpen=obj.innerHTML.match(/(<[a-zA-Z]+[^>]*?>)/g);
	//alert('Opening Tags: \n\n'+tagsOpen.join("\n"))

	var tagsOpen=obj.innerHTML.match(/(<\/[a-zA-Z]+[^>]*?>)/g);
	//alert('Closing Tags: \n\n'+tagsOpen.join("\n"))
	myInnerHTML = obj.innerHTML.replace(/(<[a-zA-Z]+[^>]*?>)/g, "**=");
	myInnerHTML = myInnerHTML.replace(/(<\/[a-zA-Z]+[^>]*?>)/g, "=**");
	alert(myInnerHTML);
}
/*
alert('
**=Heading=**
**=Hello **=World=**.=**
**=**=red=** **=blue=**  **=green=**=**
');
*/
/*]]>*/
</script>

</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now