Skip to content

Instantly share code, notes, and snippets.

@iegik
Created February 20, 2012 09:51
Show Gist options
  • Select an option

  • Save iegik/1868611 to your computer and use it in GitHub Desktop.

Select an option

Save iegik/1868611 to your computer and use it in GitHub Desktop.
JavaScript: Code hightlighter
/*
.js-type {font-weight:700}
.js-variable, .js-type{color: #228}
.js-keyword{color: #708}
.js-string {color:#a22}
.js-punctuation {color:#666}
.js-atom {color:#281}
.js-comment, .js-comment * {color: #A70!important}
*/
String.prototype.hightlight = function(){
return this
// операторы
.replace(/\b(var|function|typeof|new|return|if|for|in|while|break|do|continue|switch|case|try|catch)([^a-z0-9\$_])/g,
'<span class="js-keyword">$1</span>$2')
// типы данных, объекты
.replace(/\b(RegExp|Boolean|Number|String|Array|Object|Function|this|true|false|NaN|undefined|null|Infinity)([^a-z0-9\$_])/g,
'<span class="js-type">$1</span>$2')
// скобки
.replace(/(\{|\}|\]|\[|\|)/gi,'<span class="js-punctuation">$1</span>')
// комментарии
.replace(/(\/\*[^]*?\*\/|(\/\/)[^\n\r]+)/gim,'<span class="js-comment">$1</span>')
// строки
.replace(/('.*?')/g,'<span class="js-string">$1</span>')
// названия функций
.replace(/([a-z\_\$][a-z0-9_]*)\(/gi,'<a name="$1" class="js-variable">$1</a>(')
// числа
.replace(/(0x[0-9a-f]*|\b(\d*\.)?([\d]+(e-?[0-9]*)?)\b)/gi,'<span class="js-atom">$1</span>')//|(0x[0-9abcdefx]*)
// табуляция (2 пробела)
.replace(/\t/g,' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment