Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20870 > unrolled thread

Regex match

Started byandrew.perrins@gmail.com
First post2013-08-05 14:16 -0700
Last post2013-08-05 22:45 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Regex match andrew.perrins@gmail.com - 2013-08-05 14:16 -0700
    Re: Regex match "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-08-05 23:45 +0200
    Re: Regex match Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-08-05 22:45 +0100

#20870 — Regex match

Fromandrew.perrins@gmail.com
Date2013-08-05 14:16 -0700
SubjectRegex match
Message-ID<61905f9d-bebd-42c7-a474-4d5f3db23bd3@googlegroups.com>
This is not strictly a js question, but I'm hoping I might get some feedback.

Succinctly, my question is, why does this provide a match:

 'D123'.match(/\d+/);  // -> result is ["123"]

whereas this does not provide a match:

 'D123'.match(/\d*/)   // -> [""]


If I add the "global" flag on the second regex, it's true, I get the same result as one of the matches.  But I'd expect that, since the "+" and "*" are both greedy, that they should each return the string of digits as a result with no flags.

Any help appreciated; thanks.

[toc] | [next] | [standalone]


#20871

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-08-05 23:45 +0200
Message-ID<XnsA213F199B82ABeejj99@194.109.133.133>
In reply to#20870
 wrote on 05 aug 2013 in comp.lang.javascript:

> Succinctly, my question is, why does this provide a match:
> 
>  'D123'.match(/\d+/);  // -> result is ["123"]
> 
> whereas this does not provide a match:
> 
>  'D123'.match(/\d*/)   // -> [""]

Oh, but it DOES provide a match!!!!

The first match of \d* 
is the empty string section in front of the 'D'.

This will explain it to you:

alert( 'D123'.replace(/\d+/,'x') ); // Dx

and

alert( 'D123'.replace(/\d*/,'x') ); // xD123




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#20872

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2013-08-05 22:45 +0100
Message-ID<0.bdc75a70bac30fc8fb9a.20130805224515BST.87y58fq0b8.fsf@bsb.me.uk>
In reply to#20870
andrew.perrins@gmail.com writes:

> This is not strictly a js question, but I'm hoping I might get some feedback.
>
> Succinctly, my question is, why does this provide a match:
>
>  'D123'.match(/\d+/);  // -> result is ["123"]
>
> whereas this does not provide a match:
>
>  'D123'.match(/\d*/)   // -> [""]

That looks like a match to me (and a correct one at that).

> If I add the "global" flag on the second regex, it's true, I get the
> same result as one of the matches.  But I'd expect that, since the "+"
> and "*" are both greedy, that they should each return the string of
> digits as a result with no flags.

Being greedy does not mean "skip over a short match to find a longer
one".  Here, \d* matches zero character right off the mark, and that's
the match you get.  Making the match global gives you all the others.

-- 
Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web