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


Groups > comp.lang.javascript > #21138

Re: Recommendation for new RegExp (Array-extra-like) methods

Message-ID <1458401.hGLh0OfnHy@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-09-01 15:25 +0200
Subject Re: Recommendation for new RegExp (Array-extra-like) methods
Newsgroups comp.lang.javascript
References <ac07e89f-90ee-4b6d-a524-5adfadd0cb94@googlegroups.com> <3573991.qt74eh6WeI@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:

> You might instead find my jsx.regexp.RegExp useful, which among other
> features allows you to name subpatterns and access by that name the
> corresponding matches:
> 
>   /* ?<Perl> style; also supports ?'Perl' style and ?P<ython> style */
>   var regex = new jsx.regexp.RegExp("^(?<first>.+?): (?<second>.+)$",
>   "mg"),
>       str = "key1: val1\nkey2: key2",
>       match;
> 
>   while ((match = regex.exec(str)))
>   {
>     console.log(match.groups["first"], match.groups["second"]);
>     console.log(match[1], match[2]);
>   }
> 
> “regex” holds an augmented RegExp instance then, whose inherited exec()
> method has been overridden in the instance to return an augmented
> array-like object if necessary (otherwise just the inherited method is
> called, and its return value returned):
> 
>       /**
>        * @function
>        */
>       exec: (function () {
>         var rx2;
> 
>         /**
>          * @param {string} s
>          * @param {jsx.regexp.RegExp} rx
>          *   Instance to be tested
>          * @return {boolean}
                     ^^^^^^^^^
>          *   <code>true</code> if <var>rx</var> has been constructed
>          *   using this constructor, <code>false</code> otherwise.

JFTR: As you can see further below, this is a documentation bug that will be 
fixed in the next revision.  The return value of the custom method is 
exactly what the original RegExp.prototype.exec() method would have had 
returned (“null” or a reference to an Array-like object), augmented with 
custom properties if there is a match.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Recommendation for new RegExp (Array-extra-like) methods brettz9@gmail.com - 2013-08-28 07:51 -0700
  Re: Recommendation for new RegExp (Array-extra-like) methods Andreas Bergmaier <andber93@web.de> - 2013-08-28 17:16 +0200
    Re: Recommendation for new RegExp (Array-extra-like) methods brettz9@gmail.com - 2013-08-28 08:21 -0700
      Recommendation for array-like methods on Iterator; Was Re: Recommendation for new RegExp (Array-extra-like) methods brettz9@gmail.com - 2013-08-28 16:30 -0700
  Re: Recommendation for new RegExp (Array-extra-like) methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-30 16:00 +0200
    Re: Recommendation for new RegExp (Array-extra-like) methods Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-01 15:25 +0200

csiph-web