Rumah > hujung hadapan web > tutorial js > XRegExp 0.2: Now With Named Capture_js面向对象

XRegExp 0.2: Now With Named Capture_js面向对象

WBOY
Lepaskan: 2016-05-16 19:07:51
asal
1242 orang telah melayarinya

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

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.

Highlights

  • Comprehensive named capture support (New)
  • Supports regex literals through the <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">addFlags</font> method (New)
  • Free-spacing and comments mode (<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">x</font>)
  • Dot matches all mode (<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">s</font>)
  • 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 <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(<name>…)</name></font> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\k<name></name></font> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">${name}</font> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">result.name</font>
.NET <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?<name>…)</name></font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?'name'…)</font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\k<name></name></font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\k'name'</font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">${name}</font> <font size="2"><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088">Matcher.<wbr>Groups('name')</wbr></font></font>
Perl 5.10 (beta) <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?<name>…)</name></font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?'name'…)</font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\k<name></name></font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\k'name'</font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\g{name}</font>
<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">$+{name}</font> ??
Python <nobr><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?P<name>…)</name></font></nobr> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">(?P=name)</font> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">\g<name></name></font> <font size="2"><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088">result.<wbr>group('name')</wbr></font></font>
PHP preg (PCRE) (.NET, Perl, and Python styles) <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">$regs['name']</font> <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">$result['name']</font>

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 "<font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">k</font>" 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:

<span class="comment"><font color="#238b23">// Add named capture support using the XRegExp constructor</font></span>
var repeatedWords = new XRegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

<span class="comment"><font color="#238b23">// Add named capture support using RegExp, after overriding the native constructor</font></span>
XRegExp.overrideNative();
var repeatedWords = new RegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

<span class="comment"><font color="#238b23">// Add named capture support to a regex literal</font></span>
var repeatedWords = /\b (<word> \w+ ) \s+ \k<word> \b/.addFlags("gixk");

var data = "The the test data.";

<span class="comment"><font color="#238b23">// Check if data contains repeated words</font></span>
var hasDuplicates = repeatedWords.test(data);
<span class="comment"><font color="#238b23">// hasDuplicates: true</font></span>

<span class="comment"><font color="#238b23">// Use the regex to remove repeated words</font></span>
var output = data.replace(repeatedWords, "${word}");
<span class="comment"><font color="#238b23">// output: "The test data."</font></span>
</word></word></word></word></word></word>
Salin selepas log masuk

In the above code, I've also used the <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">x</font> flag provided by XRegExp, to improve readability. Note that the <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">addFlags</font> method can be called multiple times on the same regex (e.g., <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">/pattern/g.addFlags("k").addFlags("s")</font>), 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);
<span class="comment"><font color="#238b23">/* The result:
parts.protocol: "http"
parts.host: "microsoft.com"
parts.path: "/path/to/file"
parts.query: "q=1" */</font></span>

<span class="comment"><font color="#238b23">// Named backreferences are also available in replace() callback functions as properties of the first argument</font></span>
var newUrl = url.replace(urlParser, function(match){
	return match.replace(match.host, "yahoo.com");
});
<span class="comment"><font color="#238b23">// newUrl: "http://yahoo.com/path/to/file?q=1"</font></span>

</query></path></host></protocol>
Salin selepas log masuk

Note that XRegExp's named capture functionality does not support deprecated JavaScript features including the <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">lastMatch</font> property of the global <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">RegExp</font> object and the <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">RegExp.prototype.compile()</font> method.

Singleline (s) and extended (x) modes

The other non-native flags XRegExp supports are <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">s</font> (singleline) for "dot matches all" mode, and <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">x</font> (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 <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">x</font> flag, now allows whitespace between a regex token and its quantifier (quantifiers are, e.g., <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">+</font>, <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">*?</font>, or <font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">{1,3}</font>). Although the previous version's handling/limitation in this regard was documented, it was atypical compared to other regex libraries. This has been fixed.

The code

<span class="comment"><font color="#238b23">/* XRegExp 0.2.2; MIT License
By Steven Levithan <http:>
----------
Adds support for the following regular expression features:
- Free-spacing and comments ("x" flag)
- Dot matches all ("s" flag)
- Named capture ("k" flag)
 - Capture: (<name>...)
 - Backreference: \k<name>
 - In replacement: ${name}
 - Stored at: result.name
*/</name></name></http:></font></span>

<span class="comment"><font color="#238b23">/* Protect this from running more than once, which would break its references to native functions */</font></span>
if (window.XRegExp === undefined) {
	var XRegExp;
	
	(function () {
		var native = {
			RegExp: RegExp,
			exec: RegExp.prototype.exec,
			match: String.prototype.match,
			replace: String.prototype.replace
		};
		
		XRegExp = function (pattern, flags) {
			return native.RegExp(pattern).addFlags(flags);
		};
		
		RegExp.prototype.addFlags = function (flags) {
			var pattern = this.source,
				useNamedCapture = false,
				re = XRegExp._re;
			
			flags = (flags || "") + native.replace.call(this.toString(), /^[\S\s]+\//, "");
			
			if (flags.indexOf("x") > -1) {
				pattern = native.replace.call(pattern, re.extended, function ($0, $1, $2) {
					return $1 ? ($2 ? $2 : "(?:)") : $0;
				});
			}
			
			if (flags.indexOf("k") > -1) {
				var captureNames = [];
				pattern = native.replace.call(pattern, re.capturingGroup, function ($0, $1) {
					if (/^\((?!\?)/.test($0)) {
						if ($1) useNamedCapture = true;
						captureNames.push($1 || null);
						return "(";
					} else {
						return $0;
					}
				});
				if (useNamedCapture) {
					<span class="comment"><font color="#238b23">/* Replace named with numbered backreferences */</font></span>
					pattern = native.replace.call(pattern, re.namedBackreference, function ($0, $1, $2) {
						var index = $1 ? captureNames.indexOf($1) : -1;
						return index > -1 ? "\\" + (index + 1).toString() + ($2 ? "(?:)" + $2 : "") : $0;
					});
				}
			}
			
			<span class="comment"><font color="#238b23">/* If "]" is the leading character in a character class, replace it with "\]" for consistent
			cross-browser handling. This is needed to maintain correctness without the aid of browser sniffing
			when constructing the regexes which deal with character classes. They treat a leading "]" within a
			character class as a non-terminating, literal character, which is consistent with IE, .NET, Perl,
			PCRE, Python, Ruby, JGsoft, and most other regex engines. */</font></span>
			pattern = native.replace.call(pattern, re.characterClass, function ($0, $1) {
				<span class="comment"><font color="#238b23">/* This second regex is only run when a leading "]" exists in the character class */</font></span>
				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;
			<span class="comment"><font color="#238b23">/* Preserve capture names if adding flags to a regex which has already run through addFlags("k") */</font></span>
			} else if (this._captureNames) {
				regex._captureNames = this._captureNames.valueOf();
			}
			
			return regex;
		};
		
		String.prototype.replace = function (search, replacement) {
			<span class="comment"><font color="#238b23">/* If search is not a regex which uses named capturing groups, just run the native replace method */</font></span>
			if (!(search instanceof native.RegExp && search._captureNames)) {
				return native.replace.apply(this, arguments);
			}
			
			if (typeof replacement === "function") {
				return native.replace.call(this, search, function () {
					<span class="comment"><font color="#238b23">/* Convert arguments[0] from a string primitive to a string object which can store properties */</font></span>
					arguments[0] = new String(arguments[0]);
					<span class="comment"><font color="#238b23">/* Store named backreferences on the first argument before calling replacement */</font></span>
					for (var i = 0; i <font color="#238b23">/* Numbered backreference or special variable */</font>
						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);
								<span class="comment"><font color="#238b23">/* Numbered backreference */</font></span>
								default:
									<span class="comment"><font color="#238b23">/* 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, it's the string "$10" */</font></span>
									var literalNumbers = "";
									$1 = +$1; <span class="comment"><font color="#238b23">/* Cheap type-conversion */</font></span>
									while ($1 > search._captureNames.length) {
										literalNumbers = $1.toString().match(/\d$/)[0] + literalNumbers;
										$1 = Math.floor($1 / 10); <span class="comment"><font color="#238b23">/* Drop the last digit */</font></span>
									}
									return ($1 ? args[$1] : "$") + literalNumbers;
							}
						<span class="comment"><font color="#238b23">/* Named backreference */</font></span>
						} else if ($2) {
							<span class="comment"><font color="#238b23">/* What does "${name}" mean?
							- Backreference to named capture "name", if it exists
							- Else, it's the string "${name}" */</font></span>
							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 <font color="#238b23">/* Regex syntax parsing with support for escapings, character classes, and various other context and cross-browser issues */</font>
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]|$))*]?|\((?=\?))+|\((?:)?/g,
	namedBackreference: /(?:[^\\[]+|\\(?:[^k]|$)|\[\^?]?(?:[^\\\]]+|\\(?:[\S\s]|$))*]?|\\k(?!))+|\\k(\d*)/g,
	replacementVariable: /(?:[^$]+|\$(?![1-9$&`']|{[$\w]+}))+|\$(?:([1-9]\d*|[$&`'])|{([$\w]+)})/g
};

XRegExp.overrideNative = function () {
	<span class="comment"><font color="#238b23">/* Override the global RegExp constructor/object with the XRegExp constructor. This precludes accessing
	properties of the last match via the global RegExp object. However, those properties are deprecated as
	of JavaScript 1.5, and the values are available on RegExp instances or via RegExp/String methods. It also
	affects the result of (/x/.constructor == RegExp) and (/x/ instanceof RegExp), so use with caution. */</font></span>
	RegExp = XRegExp;
};

<span class="comment"><font color="#238b23">/* indexOf method from Mootools 1.11; MIT License */</font></span>
Array.prototype.indexOf = Array.prototype.indexOf || function (item, from) {
	var len = this.length;
	for (var i = (from 
<p>You can <a href="http://stevenlevithan.com/regex/xregexp/xregexp.js"><strong><font color="#3399cc">download it</font></strong></a>, or get the <a href="http://stevenlevithan.com/regex/xregexp/xregexp-min.js"><strong><font color="#3399cc">packed version</font></strong></a> (2.7 KB).</p>
<p>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.</p>
<p>Finally, note that the <code><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">XRE</font></code> object from v0.1 has been removed. XRegExp now only creates one global variable: <code><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">XRegExp</font></code>. To permanently override the native <code><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">RegExp</font></code> constructor/object, you can now run <code><font style="BACKGROUND-COLOR: #f5f5ff" color="#000088" size="2">XRegExp.overrideNative();</font></code></p>
<div style="CLEAR: both"></div>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan