RegEx Question: No stingy matching? 
Author Message
 RegEx Question: No stingy matching?

Hi,

In perl, I can make matching stingy with a ? after the * so that
this....

  t += lines[i].replace(/(.*?)\/\/.*$/g, "$1");

Will match everything up to the FIRST set of forward slashes and add it
to t

In perl, if there was no ? after the .* the $1 would contain everything
up to the LAST set of forward slashes, because matches are greedy in
perl unless you include the question mark.

Greedy matches seem to be the case in JavaScript regardless of if you
include the question mark or not.   Is this true?  Is there some other
mechanism for getting stingy matches?

Thanks if you can help....

   -John Repici



Fri, 28 Feb 2003 11:56:50 GMT  
 RegEx Question: No stingy matching?

Up to MS JScript 5.0 the regexp only had greedy matches.
JScript 5.5 also has non-greedy matches (as they are called there)

For Netscape I don't know.

Hans Kesting


Quote:
> Hi,

> In perl, I can make matching stingy with a ? after the * so that
> this....

>   t += lines[i].replace(/(.*?)\/\/.*$/g, "$1");

> Will match everything up to the FIRST set of forward slashes and add it
> to t

> In perl, if there was no ? after the .* the $1 would contain everything
> up to the LAST set of forward slashes, because matches are greedy in
> perl unless you include the question mark.

> Greedy matches seem to be the case in JavaScript regardless of if you
> include the question mark or not.   Is this true?  Is there some other
> mechanism for getting stingy matches?

> Thanks if you can help....

>    -John Repici



Fri, 28 Feb 2003 15:57:53 GMT  
 RegEx Question: No stingy matching?

Hans,

Thanks for clarifying that.  Is JScript 5.5 the same as I.E. 5.5?

I use I.E. 5.5 and Netscape 4.75 and one of them (don't remember which one)
was not implementing this construct.  For now, I've switched to a more
traditional (in JavaScript anyway) use of  indexOf() and substring() methods
to do the same work.

Thanks again Hans.  Take care now.

    -John

Quote:

> Up to MS JScript 5.0 the regexp only had greedy matches.
> JScript 5.5 also has non-greedy matches (as they are called there)

> For Netscape I don't know.

> Hans Kesting



> > Hi,

> > In perl, I can make matching stingy with a ? after the * so that
> > this....

> >   t += lines[i].replace(/(.*?)\/\/.*$/g, "$1");

> > Will match everything up to the FIRST set of forward slashes and add it
> > to t

> > In perl, if there was no ? after the .* the $1 would contain everything
> > up to the LAST set of forward slashes, because matches are greedy in
> > perl unless you include the question mark.

> > Greedy matches seem to be the case in JavaScript regardless of if you
> > include the question mark or not.   Is this true?  Is there some other
> > mechanism for getting stingy matches?

> > Thanks if you can help....

> >    -John Repici



Fri, 28 Feb 2003 23:24:38 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. regex question on multiple word matches

2. Filename pattern matching using regex

3. RegEx object - Pattern Matching

4. RegExp question: match within another match

5. regex question about 'length'

6. Important RegEx Question

7. RegEx howto question

8. Anchored Regex search and replace question

9. Pattern Matching question..

10. RegEx to capture script blocks?

11. RegEx 5.5 under Win95

12. REGEX Help

 

 
Powered by phpBB® Forum Software