Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Tuesday, May 29, 2012

Javascript regular expressions

Do capturing thus

r = /score=(\d\d)/;
a = r.exec("players=2;score=98;duration=2:09:03");
// a[0] is the whole match (score=98)
// a[1] is capture group 1 (98) like $1 in Perl
// if there are more groups, they are in subsequent array items

w3schools says exec returns the matched text, but I always get an array as described above.

If you define the regexp as global (/g) then you pick up successive matches in the string thusly

while (a = r.exec(string)) { ... }

(the regexp object must I suppose have state so that it can remember how far it's got through string on each iteration.)

The /.../ regexp syntax is part of the JS standard and perfectly respectable. Using it creates a RegExp object, of which exec() is one of the methods.

Followers

Blog Archive