Skip to content

Instantly share code, notes, and snippets.

@zdying
Created August 14, 2014 05:38
Show Gist options
  • Save zdying/2fb94a505396670b535c to your computer and use it in GitHub Desktop.
Save zdying/2fb94a505396670b535c to your computer and use it in GitHub Desktop.
simple highlighter
function highlight(str){
var colors = ['blue','rgb(9, 197, 9)','red','lightgray'];
var reg = /(var|this|new|return|function)|([\"'].*?[\"'])|([+-]?\d+(?:\.\d+)?)|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
var res = str.replace(reg, function(match){
var args = [].slice.call(arguments,1);
return '<span style="color:' + colors[args.indexOf(match)] + '">' + match + '</span>';
});
return res;
}
// var pre = document.getElementsByTagName('pre')[0];
// pre.innerHTML = highlight(pre.innerHTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment