Solved

JavaScript: Test for same number of opening and closing tags

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

856 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