Solved

JavaScript: Test for same number of opening and closing tags

Posted on 2011-02-21
1
260 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 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…

749 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