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

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Thu Mar 10 05:41:50 CST 2005


Here are the two use-cases which I think clarifies things:

case 1:
var myreg = /<x{0,1}(?!p).*>/;
alert("<xp>".match(myreg));

1. match "<"  --> ok
2. match "x" once --> ok
3. look ahead, see p --> 2 cannot match --> rollback 2
2. macth x zero times. --> ok
3. look ahead (see "x") --> it is not p --> ok		
4. match anything else ("xp")
5. match ">"
6 return "<xp>"
		
case 2:
var myreg = /<\/{0,1}(?!(p))
"</p>".match(myreg);
		
1. match "<" --> ok
2. match "/" --> ok
3. look ahead --> see a p --> roll back 2
2. match "/" zero times. --> ok
3. look ahead there is "/" and it is not "p" --> OK
4. nothing else to match
so return "<"


Thank you again for all your help.

Have a nice day (there's a snowstorm here :( )
volkan.

On Thu, 10 Mar 2005 13:20:56 +0200, VOLKAN ÖZÇELİK
<volkan.ozcelik at gmail.com> wrote:
> I think I am beginning to understand.
> 
> I'll post my final thoughts when I am clear on the subject.
> 
> Thank you for all for your help.
> 
> Cheers.
> 
> On Thu, 10 Mar 2005 13:12:24 +0200, VOLKAN ÖZÇELİK
> <volkan.ozcelik at gmail.com> wrote:
> > sorry. I looked at my posting, my original reg ex was wrong.
> >
> > pardon me.
> >
> > Could you please consider the following and discard my previous post.
> >
> > sorry for the confusion.
> >
> > ----
> >
> > 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?
> >
> > ---
> >
> > On Thu, 10 Mar 2005 12:27:32 +0200, VOLKAN ÖZÇELİK
> > <volkan.ozcelik at gmail.com> wrote:
> > > /<\/?(?!p)[\s\S]*?>/ig
> > >
> > > what about the last ">" ?
> > >
> > > you tested it as
> > >
> > > '</p>'.match(/<\/?(?!p)/)
> > >
> > > but it should be
> > >
> > > '</p>'.match(/<\/?(?!p)>/)
> > >
> > > If the last ">" weren't there you were right. But now it should
> > > account for the final ">" as well isn't it?
> > >
> > > Cheers,
> > > Volkan.
> > >
> > >
> > > ---------- Forwarded message ----------
> > > From: Jeffery To <jeffery.to at gmx.net>
> > > Date: Thu, 10 Mar 2005 04:50:07 -0500
> > > Subject: Re: [thelist]  Regular Expression - Is it a bug or am I
> > > missing something very obvious?
> > > To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
> > >
> > > 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.
> > > --
> > >
> > > * * Please support the community that supports you.  * *
> > > http://evolt.org/help_support_evolt/
> > >
> > > For unsubscribe and other options, including the Tip Harvester
> > > and archives of thelist go to: http://lists.evolt.org
> > > Workers of the Web, evolt !
> > >
> >
>


More information about the thelist mailing list