About

CSS (3)

JavaScript (6)

Web browsers (2)

 

Blog

JavaScript

A little regular expression to remove all HTML tags from a string.

Input code
<h2>About</h2>
<p>Here you will find posts about news, trends and developing for internet, 
	mainly focusing on browsers and web user interfaces.</p>
<p><a href="/about">Read more</a></p>
Output text
About
Here you will find posts about news, trends and developing for internet, 
	mainly focusing on browsers and web user interfaces.
Read more
Remove all HTML tags from the input code.

The script

function removeHTMLTags(){
	if(document.getElementById && document.getElementById("input-code")){
		var strInputCode = document.getElementById("input-code").innerHTML;
		/* 
			This line is optional, it replaces escaped brackets with real ones, 
			i.e. &lt; is replaced with < and &gt; is replaced with >
		*/	
		strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
			return (p1 == "lt")? "<" : ">";
		});
		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
	}	
}