XRegExp 0.2: Now With Named Capture

2018-10-15 17:30

阅读:529

Update: A beta version of XRegExp 0.3 is now available as part of theRegexPal download package.

JavaScripts regular expression flavor doesnt 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 havent seen the previous version, make sure to check out my post onXRegExp 0.1, because not all of the documentation is repeated below.

Highlights 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. Ive compiled the following table based on my understanding of the regex support of the libraries in question. XRegExps syntax is included at the top.

Library Capture Backreference In replacement Stored at XRegExp (<name>…) \k<name> ${name} (?<name>…) (?name…) \k<name> \kname ${name} Matcher.Groups(name) Perl 5.10 (beta) (?<name>…) (?name…) \k<name> \kname \g{name} $+{name} ?? Python (?P<name>…) (?P=name) \g<name> result.group(name) PHP preg (PCRE) (.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 likeRegexBuddy

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 XRegExp.overrideNative(); 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, Ive 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 Id 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, seeparseUri):

var url = // Named backreferences are also available in replace() callback functions as properties of the first argument// newUrl:

Note that XRegExps named capture functionality does not supportdeprecated JavaScript featuresincluding the lastMatch property of the global RegExp object and the 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 myXRegExp 0.1post. 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 versions handling/limitation in this regard was documented, it was atypical compared to other regex libraries. This has been fixed.

The code/* XRegExp 0.2.2; MIT License By Steven Levithan < /* Protect this from running more than once, which would break its references to native functions *//* Replace named with numbered backreferences */ pattern = native.replace.call(pattern, re.characterClass, function ($0, $1) { /* This second regex is only run when a leading ] exists in the character class */ return $1 ? native.replace.call($0, /^(\[\^?)]/, $1\\]) : $0; }); if (flags.indexOf(s) > -1) { pattern = native.replace.call(pattern, re.singleline, function ($0) { return $0 === . ? [\\S\\s] : $0; }); } var regex = native.RegExp(pattern, native.replace.call(flags, /[sxk]+/g, )); if (useNamedCapture) { regex._captureNames = captureNames; /* Preserve capture names if adding flags to a regex which has already run through addFlags(k) */ } else if (this._captureNames) { regex._captureNames = this._captureNames.valueOf(); } return regex; }; String.prototype.replace = function (search, replacement) { /* If search is not a regex which uses named capturing groups, just run the native replace method */ if (!(search instanceof native.RegExp && search._captureNames)) { return native.replace.apply(this, arguments); } if (typeof replacement === function) { return native.replace.call(this, search, function () { /* Convert arguments[0] from a string primitive to a string object which can store properties */ arguments[0] = new String(arguments[0]); /* Store named backreferences on the first argument before calling replacement */ for (var i = 0; i < search._captureNames.length; i++) { if (search._captureNames[i]) arguments[0][search._captureNames[i]] = arguments[i + 1]; } return replacement.apply(window, arguments); }); } else { return native.replace.call(this, search, function () { var args = arguments; return native.replace.call(replacement, XRegExp._re.replacementVariable, function ($0, $1, $2) { /* Numbered backreference or special variable */ if ($1) { switch ($1) { case $: return $; case &: return args[0]; case `: return args[args.length - 1].substring(0, args[args.length - 2]); case : return args[args.length - 1].substring(args[args.length - 2] + args[0].length); /* Numbered backreference */ default: /* What does $10 mean? - Backreference 10, if at least 10 capturing groups exist - Backreference 1 followed by 0, if at least one capturing group exists - Else, its the string $10 */ var literalNumbers = ; $1 = +$1; /* Cheap type-conversion */ while ($1 > search._captureNames.length) { literalNumbers = $1.toString().match(/\d$/)[0] + literalNumbers; $1 = Math.floor($1 / 10); /* Drop the last digit */ } return ($1 ? args[$1] : $) + literalNumbers; } /* Named backreference */ } else if ($2) { /* What does ${name} mean? - Backreference to named capture name, if it exists - Else, its the string ${name} */ var index = search._captureNames.indexOf($2); return index > -1 ? args[index + 1] : $0; } else { return $0; } }); }); } }; RegExp.prototype.exec = function (str) { var result = native.exec.call(this, str); if (!(this._captureNames && result && result.length > 1)) return result; for (var i = 1; i < result.length; i++) { var name = this._captureNames[i - 1]; if (name) result[name] = result[i]; } return result; }; String.prototype.match = function (regexp) { if (!regexp._captureNames regexp.global) return native.match.call(this, regexp); return regexp.exec(this); }; })(); } /* Regex syntax parsing with support for escapings, character classes, and various other context and cross-browser issues */ XRegExp._re = { extended: /(?:[^[#\s\\]+\\(?:[\S\s]$)\[\^?]?(?:[^\\\]]+\\(?:[\S\s]$))*]?)+(\s*#[^\n\r]*\s*\s+)([?*+]{\d+(?:,\d*)?})?/g, singleline: /(?:[^[\\.]+\\(?:[\S\s]$)\[\^?]?(?:[^\\\]]+\\(?:[\S\s]$))*]?)+\./g, characterClass: /(?:[^\\[]+\\(?:[\S\s]$))+\[\^?(]?)(?:[^\\\]]+\\(?:[\S\s]$))*]?/g, capturingGroup: /(?:[^[(\\]+\\(?:[\S\s]$)\[\^?]?(?:[^\\\]]+\\(?:[\S\s]$))*]?\((?=\?))+\((?:<([$\w]+)>)?/g, namedBackreference: /(?:[^\\[]+\\(?:[^k]$)\[\^?]?(?:[^\\\]]+\\(?:[\S\s]$))*]?\\k(?!<[$\w]+>))+\\k<([$\w]+)>(\d*)/g, replacementVariable: /(?:[^$]+\$(?![1-9$&`]{[$\w]+}))+\$(?:([1-9]\d*[$&`]){([$\w]+)})/g }; XRegExp.overrideNative = function () { RegExp = XRegExp; }; /* indexOf method from Mootools 1.11; MIT License */ Array.prototype.indexOf = Array.prototype.indexOf function (item, from) { var len = this.length; for (var i = (from < 0) ? Math.max(0, len + from) : from 0; i < len; i++) { if (this[i] === item) return i; } return -1; };

You candownload it, or get thepacked version(2.7 KB).

XRegExp has been tested in IE 5.5–7, Firefox 2.0.0.4, 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();


评论


亲,登录后才可以留言!