[thelist] Fwd: Regular Expression - Is it a bug or am I missing something very obvious?
VOLKAN ÖZÇELİK
volkan.ozcelik at gmail.com
Thu Mar 10 01:29:10 CST 2005
I minimized the problem further.
/<\/(?!p)[\s\S]*?>/ig matches "</p>"
but neither
/<\/?(?!p)[\s\S]*?>/ig
nor
/<\/{0,1}(?!p)[\s\S]*?>/ig
matches "</p>". Imho the three expressions above should match "</p>".
So it is repetition-related. (the ? or {0,1} makes it fail.
Is it a problem with javascript regexp's lookahead assertion?
Can I find any further documentation and examples pointing to such
peculiarities?
Any suggestions, ideas wil be welcome.
---------- Forwarded message ----------
From: VOLKAN ÖZÇELİK <volkan.ozcelik at gmail.com>
Date: Thu, 10 Mar 2005 06:46:14 +0200
Subject: Regular Expression - Is it a bug or am I missing something
very obvious?
To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
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