...

View Full Version : Interesting RegExp challenge



bowser1111
02-22-2007, 09:47 AM
Hi... I want to use a regexp to detect if there are any HTML tags in a body of text that are NOT one of the following:

p
h1
h2
h3
h4
h5
h6
em
sub
sup
a

I want to know if any other tags are used so I can throw an error...
The use of negatives is what makes this regex hard...
Any advice is appreciated!

BonRouge
02-22-2007, 11:33 AM
How about this?
'/<(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a|\/)(|.+)>/Ui'

Philip M
02-22-2007, 07:25 PM
This seems to work:

if (/<(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a)/gi.test(lstr.value)) {

bowser1111
02-23-2007, 12:56 AM
Well, I found a way...

It's a slightly modified version of the one above. The only problem with the one above is that it gets triggered by the closing tags...

"\/?" should catch the closing tags, but the problem is (and I'm still not sure I understand why) it gets triggered by the closing tags as well. I've stopped this by having it look ahead to make sure the next character isn't the closing / either.

<\/?(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a|\/).+>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum