Created
August 14, 2014 05:38
-
-
Save zdying/2fb94a505396670b535c to your computer and use it in GitHub Desktop.
simple highlighter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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