Default non-capturing regex flag [WAS: how to create strawman proposals?]

Mike Samuel mikesamuel at gmail.com
Mon Jun 6 09:52:47 PDT 2011


2011/6/3 Kyle Simpson <getify at gmail.com>:
> I propose a /n flag for regular expressions, which would swap the default
> capturing/non-capturing behavior between ( ) and (?: ) operators (that is, (
> ) would not capture, and (?: ) would capture).
>
> The /n property would reflect on the RegExp object as `Noncapturing ==
> true`.

Can RegExp flag experimentation be done in library code?

function ExtRegExp(regexp, flags) {
  if ("string" !== typeof regexp) {
    // Convert parse tree form back to string
  }
  return RegExp(regexp, flags);
}

ExtRegExp.parse = function (regexp) {
  // Converts "^(?:foo|bar)$" to ["", ["^"], ["(?:)", ["|", "foo",
"bar"]], ["$"]]
};

ExtRegExp.n = function (regexp) {
  // Converts "^(?:foo|bar)$" to ["", ["^"], ["(?:)", ["|", "foo",
"bar"]], ["$"]]
  if ("string" === typeof regexp) { regexp = ExtRegExp.parse(regexp); }
  // Walk parse tree swapping "(?:)" nodes to capturing groups and vice-versa.
  ...
  return regexp;
};

ExtRegExp.x = function (regexp) {
  if ("string" === typeof regexp) { regexp = ExtRegExp.parse(regexp); }
  // Walk parse tree eliminating whitespace.
  ...
  return regexp;
};

// Use of n and x flags.

var myRegexp = new ExtRegExp(
    ExtRegExp.n(
        ExtRegExp.x(
            "regexp-source-here")),
    "i");


More information about the es-discuss mailing list