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

Jeffery To jeffery.to at gmx.net
Thu Mar 10 03:50:07 CST 2005


It's matching what you're telling it to match:

'</p>'.match(/<\/?(?!p)/) returns '<' because the string '<' contains 
the character '<', zero occurrences of the character '/' and is not 
followed by the character 'p' in the source string. (It is followed by 
the character '/'.)

HTH,
Jeff


VOLKAN ÖZÇELİK wrote:
> 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