You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
9.9 KiB
JavaScript
21 lines
9.9 KiB
JavaScript
//XRegExp 3.0.0-pre-20120914 <xregexp.com> MIT License
|
|
var XRegExp=function(p){function A(a,b,d){var c;if(d)if(a.__proto__)a.__proto__=e.prototype;else for(c in e.prototype)a[c]=e.prototype[c];a[g]={captureNames:b};return a}function w(a){return j.replace.call(a,/([\s\S])(?=[\s\S]*\1)/g,"")}function s(a,b){if(!e.isRegExp(a))throw new TypeError("Type RegExp expected");var d=j.exec.call(/\/([a-z]*)$/i,String(a))[1],b=b||{};b.add&&(d=w(d+b.add));b.remove&&(d=j.replace.call(d,RegExp("["+b.remove+"]+","g"),""));return a=A(RegExp(a.source,d),a[g]&&a[g].captureNames?
|
|
a[g].captureNames.slice(0):null,b.addProto)}function t(a,b){if(Array.prototype.indexOf)return a.indexOf(b);var d=a.length,c;for(c=0;c<d;++c)if(a[c]===b)return c;return-1}function u(a,b){return B.call(a)==="[object "+b+"]"}function C(a,b,d){return j.test.call(-1<d.indexOf("x")?/^(?:\s+|#.*|\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/:/^(?:\(\?#[^)]*\))*(?:[?*+]|{\d+(?:,\d*)?})/,a.slice(b))}function D(a){a=a||{};u(a,"String")&&(a=e.forEach(a,/[^\s,]+/,function(a){this[a]=!0},{}));return a}function E(a){if(!/^[\w$]$/.test(a))throw Error("Flag must be a single character A-Za-z0-9_$");
|
|
F[a]=!0}function G(a){RegExp.prototype.exec=(a?l:j).exec;RegExp.prototype.test=(a?l:j).test;String.prototype.match=(a?l:j).match;String.prototype.replace=(a?l:j).replace;String.prototype.split=(a?l:j).split;o.natives=a}function q(a){if(null==a)throw new TypeError("Cannot convert null or undefined to object");return a}var g="xregexp",e,o={astral:!1,natives:!1},j={exec:RegExp.prototype.exec,test:RegExp.prototype.test,match:String.prototype.match,replace:String.prototype.replace,split:String.prototype.split},
|
|
l={},x={},v={},y=[],H={"default":/\\(?:0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)?|[1-9]\d*|x[\dA-Fa-f]{2}|u[\dA-Fa-f]{4}|c[A-Za-z]|[\s\S])|\(\?[:=!]|[?*+]\?|{\d+(?:,\d*)?}\??|[\s\S]/,"class":/\\(?:[0-3][0-7]{0,2}|[4-7][0-7]?|x[\dA-Fa-f]{2}|u[\dA-Fa-f]{4}|c[A-Za-z]|[\s\S])|[\s\S]/},I=/\$(?:{([\w$]+)}|(\d\d?|[\s\S]))/g,J=j.exec.call(/()??/,"")[1]===p,z=RegExp.prototype.sticky!==p,F={g:!0,i:!0,m:!0,y:z},B={}.toString,n;e=function(a,b){var d={hasNamedCapture:!1,captureNames:[]},c="default",f="",i=0,h,k;if(e.isRegExp(a)){if(b!==
|
|
p)throw new TypeError("Cannot supply flags when copying a RegExp");return s(a,{addProto:!0})}a=a===p?"":String(a);b=b===p?"":String(b);k=a+"/"+b;if(!v[k]){h=a;var g=b,m;if(w(g)!==g)throw new SyntaxError("Invalid duplicate regex flag "+g);h=j.replace.call(h,/^\(\?([\w$]+)\)/,function(a,b){if(j.test.call(/[gy]/,b))throw new SyntaxError("Cannot use flag g or y in mode modifier "+a);g=w(g+b);return""});for(m=0;m<g.length;++m)if(!F[g.charAt(m)])throw new SyntaxError("Unknown regex flag "+g.charAt(m));
|
|
h={pattern:h,flags:g};a=h.pattern;for(b=h.flags;i<a.length;){do{h=a;m=b;for(var l=i,n=c,o=d,q=y.length,t=null,u=void 0,r=void 0;q--;)if(r=y[q],(r.scope===n||"all"===r.scope)&&(!r.flag||-1<m.indexOf(r.flag)))if(u=e.exec(h,r.regex,l,"sticky")){t={matchLength:u[0].length,output:r.handler.call(o,u,n,m),reparse:r.reparse};break}(h=t)&&h.reparse&&(a=a.slice(0,i)+h.output+a.slice(i+h.matchLength))}while(h&&h.reparse);h?(f+=h.output,i+=h.matchLength||1):(h=e.exec(a,H[c],i,"sticky")[0],f+=h,i+=h.length,"["===
|
|
h&&"default"===c?c="class":"]"===h&&"class"===c&&(c="default"))}v[k]={pattern:j.replace.call(f,/\(\?:\)(?=\(\?:\))|^\(\?:\)|\(\?:\)$/g,""),flags:j.replace.call(b,/[^gimy]+/g,""),captures:d.hasNamedCapture?d.captureNames:null}}k=v[k];return A(RegExp(k.pattern,k.flags),k.captures,!0)};e.prototype=RegExp();e.version="3.0.0-pre";e.addToken=function(a,b,d){var d=d||{},c=d.optionalFlags,f;d.flag&&E(d.flag);if(c){c=j.split.call(c,"");for(f=0;f<c.length;++f)E(c[f])}y.push({regex:s(a,{add:"g"+(z?"y":"")}),
|
|
handler:b,scope:d.scope||"default",flag:d.flag,reparse:d.reparse});e.cache.flush("patterns")};e.cache=function(a,b){var d=a+"/"+(b||"");return x[d]||(x[d]=e(a,b))};e.cache.flush=function(a){"patterns"===a?v={}:x={}};e.escape=function(a){return j.replace.call(q(a),/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")};e.exec=function(a,b,d,c){var f="g";if(z&&(c||b.sticky&&!1!==c))f+="y";b[g]=b[g]||{captureNames:null};f=b[g][f]||(b[g][f]=s(b,{add:f,remove:!1===c?"y":""}));f.lastIndex=d=d||0;a=l.exec.call(f,a);c&&(a&&
|
|
a.index!==d)&&(a=null);b.global&&(b.lastIndex=a?f.lastIndex:0);return a};e.forEach=function(a,b,d,c){for(var f=0,i=-1;f=e.exec(a,b,f);)d.call(c,f,++i,a,b),f=f.index+(f[0].length||1);return c};e.globalize=function(a){return s(a,{add:"g",addProto:!0})};e.install=function(a){a=D(a);!o.astral&&a.astral&&(e.cache.flush("patterns"),o.astral=!0);!o.natives&&a.natives&&G(!0)};e.isInstalled=function(a){return!!o[a]};e.isRegExp=function(a){return"[object RegExp]"===B.call(a)};e.match=function(a,b,d){var c=
|
|
b.global&&"one"!==d||"all"===d,f=(c?"g":"")+(b.sticky?"y":"");b[g]=b[g]||{captureNames:null};f=b[g][f||"noGY"]||(b[g][f||"noGY"]=s(b,{add:f,remove:"one"===d?"g":""}));a=j.match.call(q(a),f);b.global&&(b.lastIndex="one"===d&&a?a.index+a[0].length:0);return c?a||[]:a&&a[0]};e.matchChain=function(a,b){return function c(a,i){var h=b[i].regex?b[i]:{regex:b[i]},k=[],g=function(a){if(h.backref){if(!(a.hasOwnProperty(h.backref)||+h.backref<a.length))throw new ReferenceError("Backreference to undefined group: "+
|
|
h.backref);k.push(a[h.backref]||"")}else k.push(a[0])},m;for(m=0;m<a.length;++m)e.forEach(a[m],h.regex,g);return i===b.length-1||!k.length?k:c(k,i+1)}([a],0)};e.replace=function(a,b,d,c){var f=e.isRegExp(b),i=b.global&&"one"!==c||"all"===c,h=(i?"g":"")+(b.sticky?"y":""),k=b;f?(b[g]=b[g]||{captureNames:null},k=b[g][h||"noGY"]||(b[g][h||"noGY"]=s(b,{add:h,remove:"one"===c?"g":""}))):i&&(k=RegExp(e.escape(String(b)),"g"));a=l.replace.call(q(a),k,d);f&&b.global&&(b.lastIndex=0);return a};e.replaceEach=
|
|
function(a,b){var d,c;for(d=0;d<b.length;++d)c=b[d],a=e.replace(a,c[0],c[1],c[2]);return a};e.split=function(a,b,d){return l.split.call(q(a),b,d)};e.test=function(a,b,d,c){return!!e.exec(a,b,d,c)};e.uninstall=function(a){a=D(a);o.astral&&a.astral&&(e.cache.flush("patterns"),o.astral=!1);o.natives&&a.natives&&G(!1)};e.union=function(a,b){var d=/(\()(?!\?)|\\([1-9]\d*)|\\[\s\S]|\[(?:[^\\\]]|\\[\s\S])*]/g,c=[],f=0,i,h,k,l=function(a,b,d){var c=h[f-i];if(b){if(++f,c)return"(?<"+c+">"}else if(d)return"\\"+
|
|
(+d+i);return a},m;if(!u(a,"Array")||!a.length)throw new TypeError("Must provide a nonempty array of patterns to merge");for(m=0;m<a.length;++m)k=a[m],e.isRegExp(k)?(i=f,h=k[g]&&k[g].captureNames||[],c.push(j.replace.call(e(k.source).source,d,l))):c.push(e.escape(k));return e(c.join("|"),b)};l.exec=function(a){var b=this.lastIndex,d=j.exec.apply(this,arguments),c,f;if(d){!J&&(1<d.length&&-1<t(d,""))&&(c=s(this,{remove:"g"}),j.replace.call(String(a).slice(d.index),c,function(){var a=arguments.length,
|
|
b;for(b=1;b<a-2;++b)arguments[b]===p&&(d[b]=p)}));if(this[g]&&this[g].captureNames)for(f=1;f<d.length;++f)(c=this[g].captureNames[f-1])&&(d[c]=d[f]);this.global&&(!d[0].length&&this.lastIndex>d.index)&&(this.lastIndex=d.index)}this.global||(this.lastIndex=b);return d};l.test=function(a){return!!l.exec.call(this,a)};l.match=function(a){var b;if(e.isRegExp(a)){if(a.global)return b=j.match.apply(this,arguments),a.lastIndex=0,b}else a=RegExp(a);return l.exec.call(a,q(this))};l.replace=function(a,b){var d=
|
|
e.isRegExp(a),c,f,i;d?(a[g]&&(f=a[g].captureNames),c=a.lastIndex):a+="";i=u(b,"Function")?j.replace.call(String(this),a,function(){var c=arguments,e;if(f){c[0]=new String(c[0]);for(e=0;e<f.length;++e)f[e]&&(c[0][f[e]]=c[e+1])}if(d&&a.global)a.lastIndex=c[c.length-2]+c[0].length;return b.apply(p,c)}):j.replace.call(null==this?this:String(this),a,function(){var a=arguments;return j.replace.call(String(b),I,function(b,d,c){if(d){c=+d;if(c<=a.length-3)return a[c]||"";c=f?t(f,d):-1;if(c<0)throw new SyntaxError("Backreference to undefined group "+
|
|
b);return a[c+1]||""}if(c==="$")return"$";if(c==="&"||+c===0)return a[0];if(c==="`")return a[a.length-1].slice(0,a[a.length-2]);if(c==="'")return a[a.length-1].slice(a[a.length-2]+a[0].length);c=+c;if(!isNaN(c)){if(c>a.length-3)throw new SyntaxError("Backreference to undefined group "+b);return a[c]||""}throw new SyntaxError("Invalid token "+b);})});d&&(a.lastIndex=a.global?0:c);return i};l.split=function(a,b){if(!e.isRegExp(a))return j.split.apply(this,arguments);var d=String(this),c=[],f=a.lastIndex,
|
|
g=0,h,b=(b===p?-1:b)>>>0;e.forEach(d,a,function(a){a.index+a[0].length>g&&(c.push(d.slice(g,a.index)),1<a.length&&a.index<d.length&&Array.prototype.push.apply(c,a.slice(1)),h=a[0].length,g=a.index+h)});g===d.length?(!j.test.call(a,"")||h)&&c.push(""):c.push(d.slice(g));a.lastIndex=f;return c.length>b?c.slice(0,b):c};n=e.addToken;n(/\\([ABCE-RTUVXYZaeg-mopqyz]|c(?![A-Za-z])|u(?![\dA-Fa-f]{4})|x(?![\dA-Fa-f]{2}))/,function(a,b){if("B"===a[1]&&"default"===b)return a[0];throw new SyntaxError("Invalid escape "+
|
|
a[0]);},{scope:"all"});n(/\[(\^?)]/,function(a){return a[1]?"[\\s\\S]":"\\b\\B"});n(/\(\?#[^)]*\)/,function(a,b,d){return C(a.input,a.index+a[0].length,d)?"":"(?:)"});n(/\s+|#.*/,function(a,b,d){return C(a.input,a.index+a[0].length,d)?"":"(?:)"},{flag:"x"});n(/\./,function(){return"[\\s\\S]"},{flag:"s"});n(/\\k<([\w$]+)>/,function(a){var b=isNaN(a[1])?t(this.captureNames,a[1])+1:+a[1],d=a.index+a[0].length;if(!b||b>this.captureNames.length)throw new SyntaxError("Backreference to undefined group "+
|
|
a[0]);return"\\"+b+(d===a.input.length||isNaN(a.input.charAt(d))?"":"(?:)")});n(/\\(\d+)/,function(a,b){if(!("default"===b&&/^[1-9]/.test(a[1])&&+a[1]<=this.captureNames.length)&&"0"!==a[1])throw new SyntaxError("Cannot use octal escape or backreference to undefined group "+a[0]);return a[0]},{scope:"all"});n(/\(\?P?<([\w$]+)>/,function(a){if(!isNaN(a[1]))throw new SyntaxError("Cannot use integer as capture name "+a[0]);if("length"===a[1]||"__proto__"===a[1])throw new SyntaxError("Cannot use reserved word as capture name "+
|
|
a[0]);if(-1<t(this.captureNames,a[1]))throw new SyntaxError("Cannot use same name for multiple groups "+a[0]);this.captureNames.push(a[1]);this.hasNamedCapture=!0;return"("});n(/\((?!\?)/,function(a,b,d){if(-1<d.indexOf("n"))return"(?:";this.captureNames.push(null);return"("},{optionalFlags:"n"});return e}(); |