Solved

JavaScript: Test for same number of opening and closing tags

Posted on 2011-02-21
1
256 Views
Last Modified: 2012-05-11
Using JavaScript, I want to test if a string contains the same number of opening and closing matching tags.

I use REGEX to gather the tags but I don't know how to count them for matches.
<!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>Demo</title>

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

function checkTags(v) {
 // Count open tags of each time and count closed tags of each type and make sure the numbers match
 return(v.match(/(<+[^>]*?>)/g));
}
alert('Should return true:\n' + checkTags('<div><h1>Hello <strong>World</strong></h1></div>')); // Should return true because all open tags are closed
alert('Should return false:\n' + checkTags('<div><h1>Hello <strong>World</trsong></h1></div>')); // Should return false because not all open tags are correctly closed

/*]]>*/
</script>

</head>
<body>

<h1>I want to check and make sure that all open tags are closed.</h1>
<h2>The function should return true if all open tags are closed and false if not all open tags are closed.</h2>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34945904
Check this :


function checkTags(v) {
 // Count open tags of each time and count closed tags of each type and make sure the numbers match
	 var m = v.match(/(<+[^>]*?>)/g);
	 var mA = [];
	 for(var i=0;i<m.length;i++) {
	 	if(typeof mA[m[i]] == "undefined") {
	 		mA[m[i]] = 1;
	 	}
	 	else {
	 		mA[m[i]] = parseInt(mA[m[i]]) + 1;
	 	}
	 }
	 for(tag in mA) {
	 	if( tag.indexOf("</")<0 ) {
		 	endtag = tag.replace("<","</");
	 		if( mA[tag] != mA[endtag] ) return false;
	 	}
	 }
	 return true;
}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now