Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > regular expressions and multiple match

Reply
Thread Tools

regular expressions and multiple match

 
 
ast
Guest
Posts: n/a
 
      03-11-2011
hi,

let us search a.+b in string abcabcabc

var reg = /a.+b/g;
var tab = reg.exec("abcabcabc");

document.write(tab[0]);

there are 2 possible matchs:

abcab
abcabcab

javascript choose the second one. Why not. But what is the rule ?
Does javascript always provide the longest possible match ?

If i want abcab, is there something to do ?


 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-11-2011
ast wrote:

> let us search a.+b in string abcabcabc
>
> var reg = /a.+b/g;
> var tab = reg.exec("abcabcabc");
>
> document.write(tab[0]);
>
> there are 2 possible matchs:
>
> abcab
> abcabcab
>
> javascript choose the second one. Why not. But what is the rule ?
> Does javascript always provide the longest possible match ?


(There is no "javascript".)

Yes, RTFM. Regular expressions are "greedy" by default.

> If i want abcab, is there something to do ?


RTFM.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      03-11-2011
ast wrote:

> let us search a.+b in string abcabcabc
> var reg = /a.+b/g;
> var tab = reg.exec("abcabcabc");
> document.write(tab[0]);
>
> there are 2 possible matchs:
> abcab
> abcabcab
> javascript choose the second one. Why not. But what is the rule ?
> Does javascript always provide the longest possible match ?


The rule is to return the (chronologically) last found match.

> If i want abcab, is there something to do ?


'+' = repeat 1 or more times
'+?' = repeat 1 or more times, as few as possible

So:

var reg = /a.+?b/g;

Hope this helps,

--
Bart
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      03-11-2011
Bart Van der Donck wrote:

> ast wrote:
>> var reg = /a.+b/g;
>> var tab = reg.exec("abcabcabc");
>> Does javascript always provide the longest possible match ?

>
> The rule is to return the (chronologically) last found match.


Sorry, incorrect. It's indeed the longest one.

--
Bart
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      03-11-2011
"ast" <(E-Mail Removed)> writes:

> hi,
>
> let us search a.+b in string abcabcabc
>
> var reg = /a.+b/g;
> var tab = reg.exec("abcabcabc");
> document.write(tab[0]);
>
> there are 2 possible matchs:
>
> abcab
> abcabcab
>
> javascript choose the second one. Why not. But what is the rule ?


The rule is that the "+" quantifier means: Match as many as possible
of the regexp it's quantifying (in this case '.').

The "as many as possible" doesn't always mean "as long as possible",
e.g.,
/(?:..|...)+/.exec("123456789")
matches only "12345678", not all nine characters.

> Does javascript always provide the longest possible match ?


For the "+" quantifier, yes.

> If i want abcab, is there something to do ?


Use the "+?" (non-greedy) quantifier:
/a.+?b/

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expressions and multiple match ast Javascript 2 03-11-2011 10:50 AM
Regular expressions (multiple match problem) mikko.n C Programming 5 04-02-2008 10:14 PM
How to match literal backslashes read from a text file using regular expressions? cricfan@gmail.com Python 2 07-12-2005 11:53 PM
Regular expressions and parenthesis in match text Carl Cunningham Perl Misc 2 09-15-2003 11:02 AM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments