XRegExp 0.2: Now With Named Capture

Update: This version of XRegExp is outdated. See XRegExp.com for the latest, greatest version.

JavaScript's regular expression flavor doesn't support named capture. Well, says who? XRegExp 0.2 brings named capture support, along with several other new features. But first of all, if you haven't seen the previous version, make sure to check out my post on XRegExp 0.1, because not all of the documentation is repeated below.


  • Comprehensive named capture support (New)
  • Supports regex literals through the addFlags method (New)
  • Free-spacing and comments mode (x)
  • Dot matches all mode (s)
  • Several other minor improvements over v0.1

Named capture

There are several different syntaxes in the wild for named capture. I've compiled the following table based on my understanding of the regex support of the libraries in question. XRegExp's syntax is included at the top.

Library Capture Backreference In replacement Stored at
XRegExp (<name>…) \k<name> ${name} result.name
.NET (?<name>…)
${name} Matcher.Groups('name')
Perl 5.10 (beta) (?<name>…)
$+{name} $+{name}
Python (?P<name>…) (?P=name) \g<name> result.group('name')
PHP preg (PCRE 7) (.NET, Perl, and Python styles) $regs['name'] $result['name']

No other major regex library currently supports named capture, although the JGsoft engine (used by products like RegexBuddy) supports both .NET and Python syntax. XRegExp does not use a question mark at the beginning of a named capturing group because that would prevent it from being used in regex literals (JavaScript would immediately throw an "invalid quantifier" error).

XRegExp supports named capture on an on-request basis. You can add named capture support to any regex though the use of the new "k" flag. This is done for compatibility reasons and to ensure that regex compilation time remains as fast as possible in all situations.

Following are several examples of using named capture:

// Add named capture support using the XRegExp constructor
var repeatedWords = new XRegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

// Add named capture support using RegExp, after overriding the native constructor
var repeatedWords = new RegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

// Add named capture support to a regex literal
var repeatedWords = /\b (<word> \w+ ) \s+ \k<word> \b/.addFlags("gixk");

var data = "The the test data.";

// Check if data contains repeated words
var hasDuplicates = repeatedWords.test(data);
// hasDuplicates: true

// Use the regex to remove repeated words
var output = data.replace(repeatedWords, "${word}");
// output: "The test data."

In the above code, I've also used the x flag provided by XRegExp, to improve readability. Note that the addFlags method can be called multiple times on the same regex (e.g., /pattern/g.addFlags("k").addFlags("s")), but I'd recommend adding all flags in one shot, for efficiency.

Here are a few more examples of using named capture, with an overly simplistic URL-matching regex (for comprehensive URL parsing, see parseUri):

var url = "http://microsoft.com/path/to/file?q=1";
var urlParser = new XRegExp("^(<protocol>[^:/?]+)://(<host>[^/?]*)(<path>[^?]*)\\?(<query>.*)", "k");
var parts = urlParser.exec(url);
/* The result:
parts.protocol: "http"
parts.host: "microsoft.com"
parts.path: "/path/to/file"
parts.query: "q=1" */

// Named backreferences are also available in replace() callback functions as properties of the first argument
var newUrl = url.replace(urlParser, function(match){
	return match.replace(match.host, "yahoo.com");
// newUrl: "http://yahoo.com/path/to/file?q=1"

Note that XRegExp's named capture functionality does not support deprecated JavaScript features including the lastMatch property of the global RegExp object and the RegExp.prototype.compile() method.

Singleline (s) and extended (x) modes

The other non-native flags XRegExp supports are s (singleline) for "dot matches all" mode, and x (extended) for "free-spacing and comments" mode. For full details about these modifiers, see the FAQ in my XRegExp 0.1 post. However, one difference from the previous version is that XRegExp 0.2, when using the x flag, now allows whitespace between a regex token and its quantifier (quantifiers are, e.g., +, *?, or {1,3}). Although the previous version's handling/limitation in this regard was documented, it was atypical compared to other regex libraries. This has been fixed.


XRegExp 0.2.5.

XRegExp has been tested in IE 5.5–7, Firefox, Opera 9.21, Safari 3.0.2 beta for Windows, and Swift 0.2.

Finally, note that the XRE object from v0.1 has been removed. XRegExp now only creates one global variable: XRegExp. To permanently override the native RegExp constructor/object, you can now run XRegExp.overrideNative();

Update: This version of XRegExp is outdated. See XRegExp.com for the latest, greatest version.

5 thoughts on “XRegExp 0.2: Now With Named Capture”

  1. Thanks for this, it’s very helpful! just learning regex in javascript right now

  2. Hi

    I am writing some XML regular expressions and I have been thinking about how to get the named captures to stand out a bit depending on the patterns you are matching.

    I don’t know if it is worth giving different options to the user. In my case I quite like either <<Name>> or #Name# which is visible against that background.


    var xml_CharData = "(<<CDATASectionTag>><!\\[CDATA\\[" + "(<<CDATASectionContents>>(" + xml_CharNotOpenBracket + "|" + "[\]](?!\\]>)" + ")*)" + "]]>)";

    var xml_CharData = "(#CDATASectionTag#<!\\[CDATA\\[" + "(#CDATASectionContents#(" + xml_CharNotOpenBracket + "|" + "[\]](?!\\]>)" + ")*)" + "]]>)";



  3. Hi Julian,

    I can understand the desire for capture names to better stand out, but I don’t currently plan to support multiple variable name identifiers. For XRegExp’s named capture syntax, I have copied the most commonly used .NET style as closely as possible. If I added support for alternative syntax in the future, it would probably only be to copy existing implementations from other regex libraries.

    One thing that might help make the names stand out is prefixing them with “$” (e.g., "(<$CDATASectionTag><!\\[CDATA\\[)"). If you think that helps, it has a couple things going for it: It should work with XRegExp as is, and it keeps with the $n idiom for backreferences.

    If you want to change your personal copy of XRegExp to use a different capture name identifier, it should just require a simple change near the end of the XRegExp._re.capturingGroup regex. However, note that in later versions, the existing regex might be slightly tweaked or the implementation changed altogether (less likely). Also note that certain characters or sequences would break regexes. E.g., the “#” characters used in your second example would turn most of your regex into a comment if you used the “x” modifier.

    (BTW, I fixed the regexes in your above comment using the code provided in your second try. Sorry about the code stripping … use &lt; and &gt; to avoid the issue.)

  4. I like the idea of the “$” prefix and I think I’ll go with that.

    >Use &lt; and &gt; to avoid the issue.)

    Yes, I did that the second time; so as you noted, there was something wrong with the first one that just seem to affect the display of all subsequent comments.

Leave a Reply

Your email address will not be published. Required fields are marked *