[thelist] Regular Expression - Is it a bug or am I missing something very obvious?

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Wed Mar 9 22:46:14 CST 2005


Hi Community, hi RegExp lovers!

Regarding the following js reg exp.

/<\/?(?!(p))/ig;   (It's a simplified version of what I'm working on)

I think what it is supposed to do is:

1. match character < literally
2. match craracter / literally (zero or one times)
3. If there is a p immediately after do not match anything, just
rollback (i.e. negative lookahead)

so the code below

var strTest1 = "</p>";
var regEx1 = /<\/?(?!(p))/ig;
strTest1 = strTest1.replace(regEx1,"")
alert(strTest1);

should alert "</p>".
Well it doesn't. It alerts "p/>". Which means that it has found a match.

I need to swap things around to make it work: 

var strTest2 = "</p>";
regEx2 = /<(?!(\/p|p))/ig;
strTest2 = strTest2.replace(regEx2,"")
alert(strTest2);

but why on earth do I need to make things unnecessarily complicated ?!
The first one should work at least according to my humble logic!

Is it a bug? Or am I missing a really simple thing? 

Please help me before I split this PC with an axe.

TIA,
Volkan.


More information about the thelist mailing list