8000 Consider a utility method for building regular expressions with comments instead of an 'x' flag · Issue #2 · tc39/proposal-regexp-x-mode · GitHub
[go: up one dir, main page]

Skip to content
Consider a utility method for building regular expressions with comments instead of an 'x' flag #2
@brad4d

Description

@brad4d

Issue #1 points out that there is a grammar controlling regular expression parsing that will require some odd restrictions on even the comments included in the regular expression if we go through with this proposal as it is.

I also noticed that even if this proposal goes through as it is currently defined, the resulting multi-line-with-comments regular expressions you would write would be almost identical in size and readability to regular expressions generated with my example utility function below. Also, when using this utility function there is no need for odd restrictions on the content of comments.

function assembleRegExp(array, flags = undefined) {
  return new RegExp(array.join(''), flags);
}

let wordIntPairRe = assembleRegExp(
    [
      // match from the beginning, but allow leading spaces
      '^\\s*',
      // word captured to a group named "word"
      '(?<word>[a-zA-Z]+)',
      // at least one whitespace
      '\\s+',
      // integer captured to a group named "int"
      '(?<int>[0-9]+)',
      // allow trailing whitespace, but nothing else
      '\\s*$',
    ]);

console.log(wordIntPairRe);
console.log(wordIntPairRe.exec('  abc 123  '));

So, perhaps rather than proposing an 'x' flag in order to get comments, this proposal should define a method similar to the above?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0