Named capturing groups in JavaScript regex?

As far as I know there is no such thing as named capturing groups in JavaScript. What is the alternative way to get similar functionality?


ECMAScript 2018 introduces named capturing groups into JavaScript regexes.

If you need to support older browsers, you can do everything with normal (numbered) capturing groups that you can do with named capturing groups, you just need to keep track of the numbers - which may be cumbersome if the order of capturing group in your regex changes.

There are only two "structural" advantages of named capturing groups I can think of:

  • In some regex flavors (.NET and JGSoft, as far as I know), you can use the same name for different groups in your regex (see here for an example where this matters). But most regex flavors do not support this functionality anyway.

  • If you need to refer to numbered capturing groups in a situation where they are surrounded by digits, you can get a problem. Let's say you want to add a zero to a digit and therefore want to replace (d) with $10 . In JavaScript, this will work (as long as you have fewer than 10 capturing group in your regex), but Perl will think you're looking for backreference number 10 instead of number 1 , followed by a 0 . In Perl, you can use ${1}0 in this case.

  • Other than that, named capturing groups are just "syntactic sugar". It helps to use capturing groups only when you really need them and to use non-capturing groups (?:...) in all other circumstances.

    The bigger problem (in my opinion) with JavaScript is that it does not support verbose regexes which would make the creation of readable, complex regular expressions a lot easier.

    Steve Levithan's XRegExp library solves these problems.


    You can use XRegExp, an augmented, extensible, cross-browser implementation of regular expressions, including support for additional syntax, flags, and methods:

  • Adds new regex and replacement text syntax, including comprehensive support for named capture.
  • Adds two new regex flags: s , to make dot match all characters (aka dotall or singleline mode), and x , for free-spacing and comments (aka extended mode).
  • Provides a suite of functions and methods that make complex regex processing a breeze.
  • Automagically fixes the most commonly encountered cross-browser inconsistencies in regex behavior and syntax.
  • Lets you easily create and use plugins that add new syntax and flags to XRegExp's regular expression language.

  • Another possible solution: create an object containing the group names and indexes.

    var regex = new RegExp("(.*) (.*)");
    var regexGroups = { FirstName: 1, LastName: 2 };
    

    Then, use the object keys to reference the groups:

    var m = regex.exec("John Smith");
    var f = m[regexGroups.FirstName];
    

    This improves the readability/quality of the code using the results of the regex, but not the readability of the regex itself.

    链接地址: http://www.djcxy.com/p/74808.html

    上一篇: 在JavaScript中使用字符串替换使用正则表达式引用嵌套组

    下一篇: 在JavaScript正则表达式中命名捕获组?