User:Killr833/regex.js

/* JavaScript Regex Syntax Highlighter 0.1; (c) 2010 Steven Levithan ; MIT license */ var highlightJsReSyntax;(function{highlightJsReSyntax=function(y){var l="",v=0,w=0,j=[],n={quantifiable:false,type:b.NONE},t,o;y=h(y);while(t=c.regexToken.exec(y)){o=t[0];switch(o.charAt(0)){case"[":l+=""+f(o)+"";n={quantifiable:true};break;case"(":if(o.length===2){l+=d(o)}else{if(o.length===1){v++}w=w===5?1:w+1;j.push({index:l.length+14,opening:o});l+=e(o,w)}n={quantifiable:false};break;case")":if(!j.length){l+=d(")");n={quantifiable:false}}else{l+=e(")",w);n={quantifiable:!/^[=!]/.test(j[j.length-1].opening.charAt(2)),style:"g"+w};w=w===1?5:w-1;j.pop}break;case"\\":if(/^[1-9]/.test(o.charAt(1))){var u="",s=+o.slice(1);while(s>v){u=/[0-9]$/.exec(s)[0]+u;s=Math.floor(s/10)}if(s>0){l+="\\"+s+""+u}else{var p=/^\\([0-3][0-7]{0,2}|[4-7][0-7]?|[89])([0-9]*)/.exec(o);l+="\\"+p[1]+""+p[2]}}else{if(/^[0bBcdDfnrsStuvwWx]/.test(o.charAt(1))){if(/^\\[cux]$/.test(o)){l+=d(o);n={quantifiable:false};break}l+=""+o+"";if("bB".indexOf(o.charAt(1))>-1){n={quantifiable:false};break}}else{if(o==="\\"){l+=d(o)}else{l+=g(o)}}}n={quantifiable:true};break;default:if(c.quantifier.test(o)){if(n.quantifiable){var k=/^\{([0-9]+)(?:,([0-9]*))?/.exec(o);if(k&&((k[1]>65535)||(k[2]&&((k[2]>65535)||(+k[1]>+k[2]))))){l+=d(o)}else{l+=(n.style?'':"")+o+""}}else{l+=d(o)}n={quantifiable:false}}else{if(o==="|"){if(n.type===b.NONE||(n.type===b.ALTERNATOR&&!j.length)){l+=d(o)}else{l+=j.length?e("|",w):"|"}n={quantifiable:false,type:b.ALTERNATOR}}else{if("^$".indexOf(o)>-1){l+=""+o+"";n={quantifiable:false}}else{if(o==="."){l+=".";n={quantifiable:true}}else{l+=g(o);n={quantifiable:true}}}}}}}var r=0;for(var q=0;q<j.length;q++){var x=j[q].index+r;l=(l.slice(0,x)+d(j[q].opening)+l.slice(x+j[q].opening.length));r+=d("").length}return l};var c={regexToken:/\[\^?]?(?:[^\\\]]+|\\[\S\s]?)*]?|\\(?:0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)?|[1-9][0-9]*|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)|\((?:\?[:=!]?)?|(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[|\\]+|./g,charClassParts:/^(\[\^?)(]?(?:[^\\\]]+|\\[\S\s]?)*)(]?)$/,charClassToken:/[^\\-]+|-|\\(?:[0-3][0-7]{0,2}|[4-7][0-7]?|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)/g,quantifier:/^(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??$/},b={NONE:0,RANGE_HYPHEN:1,METACLASS:2,ALTERNATOR:3};function d(i){return' '+i+""}function e(j,i){return''+j+""}function h(i){return i.replace(/&(#?\w+);/g,function(k,j){return{lt:"<",gt:">",amp:"&"}[j]||k})}function g(i){return i.replace(/&/g,"&amp;").replace(//g,"&gt;")}function a(j){if(j.length>1&&j.charAt(0)==="\\"){var i=j.slice(1);if(/^c[A-Za-z]$/.test(i)){return"ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(i.charAt(1).toUpperCase)+1}else{if(/^(?:x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4})$/.test(i)){return parseInt(i.slice(1),16)}else{if(/^(?:[0-3][0-7]{0,2}|[4-7][0-7]?)$/.test(i)){return parseInt(i,8)}else{if(i.length===1&&"cuxDdSsWw".indexOf(i)>-1){return false}else{if(i.length===1){switch(i){case"b":return 8;case"f":return 12;case"n":return 10;case"r":return 13;case"t":return 9;case"v":return 11;default:return i.charCodeAt(0)}}}}}}}else{if(j!=="\\"){return j.charCodeAt(0)}}return false}function f(r){var k="",o=c.charClassParts.exec(r),i=c.charClassToken,l={rangeable:false,type:b.NONE},p,n;o.opening=o[1];o.contents=o[2];o.closing=o[3];k+=o.closing?o.opening:d(o.opening);while(p=i.exec(o.contents)){n=p[0];if(n.charAt(0)==="\\"){if(/^\\[cux]$/.test(n)){k+=d(n);l={rangeable:l.type!==b.RANGE_HYPHEN}}else{if(/^\\[dsw]$/i.test(n)){k+=""+n+"";l={rangeable:l.type!==b.RANGE_HYPHEN,type:b.METACLASS}}else{if(n==="\\"){k+=d(n)}else{k+=""+g(n)+"";l={rangeable:l.type!==b.RANGE_HYPHEN,charCode:a(n)}}}}}else{if(n==="-"){if(l.rangeable){var q=i.lastIndex,j=i.exec(o.contents);if(j){var s=a(j[0]);if((s!==false&&l.charCode>s)||l.type===b.METACLASS||/^\\[dsw]$/i.test(j[0])){k+=d("-")}else{k+=" - "}l={rangeable:false,type:b.RANGE_HYPHEN}}else{if(o.closing){k+="-"}else{k+=" - ";break}}i.lastIndex=q}else{k+="-";l={rangeable:l.type!==b.RANGE_HYPHEN}}}else{k+=g(n);l={rangeable:(n.length>1||l.type!==b.RANGE_HYPHEN),charCode:n.charCodeAt(n.length-1)}}}}return k+o.closing}});