Syntax Highlighting for JavaScript. Recognizes keywords, strings, comments (single and multi line), numbers, regexp and punctuations.
D=document,A='textarea',r='replace',p=[];D.body.innerHTML='<'+A+' id=t rows=10 cols=80>var syn="tax"; // hl</'+A+'><br><button onclick="h()">hl</button><pre id=p></pre>';function s(c,r){return '<span style="color:#'+c+'">'+(r||'$1')+'</span>'}function h(){D.getElementById('p').innerHTML=D.getElementById('t').value[r](/</g,'<')[r](/>/g,'>')[r](/\\\\/g,'<b')[r](/\\\//g,'<l')[r](/\\'/g,'<q')[r](/\\"/g,'<d')[r](/([\[({=:]\s*?)\/(?![\/\*])/g,'$1<r/')[r](/\/\*[\s|\S]*?\*\/|\/\/.*|<r\/.+?\/\w*|".*?"|'.*?'/g,function(m){return "r"+p.push(m)+">"})[r](/((&[lg]t;|[&\/+\-*=?:.,;()\[\]{}|%^!])+)/g,s('17b'))[r](/\b(break|case|catch|continue|default|delete|do|else|false|finally|for|function|if|in|instanceof|new|return|switch|this|throw|true|try|typeof|var|void|while|with)\b/gi,s('f0b'))[r](/\b(0x[\da-f]+|\d+)\b/g,s('d83'))[r](/r(\d+)>/g,function(m,n){b=p[n-1];return b.match(/^\/[\/*]/)?s('666',b):b.match(/^["']/)?s('719',b):s('6a0',b)})[r](/<r/g,"")[r](/<q/g,"\\'")[r](/<d/g,'\\"')[r](/<l/g,'\\/')[r](/<b/g,'\\\\')}h()
RD1kb2N1bWVudCxBPSd0ZXh0YXJlYScscj0ncmVwbGFjZScscD1bXTtELmJvZHkuaW5uZXJIVE1MPSc8JytBKycgaWQ9dCByb3dzPTEwIGNvbHM9ODA+dmFyIHN5bj0idGF4IjsgLy8gaGw8LycrQSsnPjxicj48YnV0dG9uIG9uY2xpY2s9ImgoKSI+aGw8L2J1dHRvbj48cHJlIGlkPXA+PC9wcmU+JztmdW5jdGlvbiBzKGMscil7cmV0dXJuICc8c3BhbiBzdHlsZT0iY29sb3I6IycrYysnIj4nKyhyfHwnJDEnKSsnPC9zcGFuPid9ZnVuY3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tinysh( code ) {
var snippets = [];
return code
// html entities
.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"')
// tabs to 4 spaces
.replace(/\t/g, ' ')
// escaped backslashes, slashes, single quotes, double quotes
.replace(/\\\\/g,'<ebs>').replace(/\\\//g,'<es>').replace(/\\'/g,'<esq>').replace(/\\"/g,'<edq>')
// regexp hint, so we can differentiate between 'replace(/foo/, bar);' and 'hours=seconds/60/60;'
.replace(/([\[({=:]\s*?)\/(?![\/\*])/g, '$1<reh>/')
// comments, regexp, strings -> save in snippets array and replace with <r#> token
.replace(/\/\*[\s|\S]*?\*\/|\/\/.*|<reh>\/.+?\/\w*|".*?"|'.*?'/g, function( match ) {
return "<r" + snippets.push( match ) + ">";
})
// punctuations
.replace(/(<|>|&|[\/+\-*=?:.,;()\[\]{}|%^!])+/g, '<span class="punct">$&</span>')
// keywords
.replace(/\b(break|case|catch|continue|default|delete|do|else|false|finally|for|function|if|in|instanceof|new|return|switch|this|throw|true|try|typeof|var|void|while|with)\b/gi,
'<span class="keyword">$1</span>'
)
// numbers (dec/hex)
.replace(/\b(0x[\da-f]+|\d+)\b/g, '<span class="number">$1</span>')
// paste snippets back in again
.replace(/<r(\d+)>/g, function( match, id ) {
var r = snippets[ id - 1 ];
var css = r.match(/^(\/\/|\/\*|-)/) ? 'comment' : r.match(/^[&']/) ? 'string' : 'regexp';
return '<span class="' + css + '">' + r + '</span>';
})
// strip all regexp hints
.replace(/<reh>/g,"")
// revert escaped characters
.replace(/<esq>/g,"\\'").replace(/<edq>/g,'\\"').replace(/<es>/g,'\\/').replace(/<ebs>/g,'\\\\');
};