?
Solved

JavaScript: Test for same number of opening and closing tags

Posted on 2011-02-21
1
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

771 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