954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regular Expression for nested html tags

Hi Guys,
I'm in need of regular expression for validating the html tags
that are closed properly,

say for example
test content see this ..
it should show an alert "tag is incorrect".
Plz help me....Guys-:)-:)

Thanks in advance

mohanrajit.88
Newbie Poster
6 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

I doubt this is possible by only using a regular expression. There are so many levels of nesting possible, I think you would need a parser instead.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

It is relatively easy to detect a which is followed by before is encountered, but that may not be an error.

Trivial counter-examples include <b>xxx</b> and <b>xxx</b> and there is an endless supply of not so trivial cases.

It would be possible to test certain tags; beyond that the reply about needing a parser is on point.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 
I doubt this is possible by only using a regular expression. There are so many levels of nesting possible, I think you would need a parser instead.


Pritaeas, you are right,it is possible by regular expression,I found the solution in php way it uses preg_match_all but i'm in need of alternative for it in java script.which should validate ,, tags that are closed properly or not.
Thanks:-/

mohanrajit.88
Newbie Poster
6 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Javescript RegExp will do _precisely_ what preg_match does.
Neither will solve the general problem, and even the three cases you have now given will not be easy.
Part of the difficulty is that "closed properly" != "closed".

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: