code = code
// keywords (the list is incomplete, wrote what came to mind)
.replace (/ (var | function | typeof | new | return | if | for | in | while | break | do | continue | switch | case) ([^ a-z0-9 \ $ _]) / gi,
'<span class = "kwrd"> $ 1 </ span> $ 2')
// any brackets
.replace (/ (\ {| \} | \] | \ [| \ |) / gi, '<span class = "kwrd"> $ 1 </ span>')
// single line comments
.replace (/ (\ / \ / [^ \ n \ r] * (\ n | \ r \ n)) / g, '<span class = "comm"> $ 1 </ span>')
// lines
.replace (/('.*?') / g, '<span class = "str"> $ 1 </ span>')
// functions (when there is a bracket after the identifier)
.replace (/ ([az \ _ \ $] [a-z0-9 _] *) \ (/ gi, '<span class = "func"> $ 1 </ span> (')
// I don’t like eight-digit tabs, let 4 spaces be better
.replace (/ \ t / g, ''); .str {color: red} / * Strings are red * /
.func {color: blue} / * User functions blue * /
.comm {color: orange} / * Comments orange * /
.kwrd {font-weight: bold} / * Keywords: bold * /
.str span {color: red; font-weight: normal} / * Everything inside the line is a string * /
.comm span {color: orange; font-weight: normal} / * Everything inside the comment - comment * / .replace (/(".*?")/ g, '<span class = "str"> $ 1 </ span>') (function ($) {
$ .fn.syntax = function () {
return this.each (function () {
var jqCode = $ (this);
var code = jqCode.text ();
code = code
.replace (/ (var | function | typeof | new | return | if | for | in | while | break | do | continue | case | switch) ([^ a-z0-9 \ $ _]) / gi, ' <span class = "kwrd"> $ 1 </ span> $ 2 ')
.replace (/ (\ {| \} | \] | \ [| \ |) / gi, '<span class = "kwrd"> $ 1 </ span>')
.replace (/ (\ / \ / [^ \ n \ r] * (\ n | \ r \ n)) / g, '<span class = "comm"> $ 1 </ span>')
.replace (/('.*?') / g, '<span class = "str"> $ 1 </ span>')
.replace (/ ([az \ _ \ $] [a-z0-9 _] *) \ (/ gi, '<span class = "func"> $ 1 </ span> (')
.replace (/ \ t / g, '');
jqCode.html (code);
});
}
}) (jQuery);
// call example
$ ('pre # code'). syntax (); // highlighting a specific block pre with id = code
$ ('pre'). syntax (); // highlight all pre function Syntax (code) {
var comments = []; // Here we collect all kamenty
var strings = []; // Here we collect all the lines
var res = []; // Here we collect all RegExp
var all = {'C': comments, 'S': strings, 'R': res};
var safe = {'<': '<', '>': '>', '&': '&'};
return code
// Mask HTML
.replace (/ [<> &] / g, function (m)
{return safe [m]; })
// Remove Kamenty
.replace (/ \ / \ * [\ s \ S] * \ * \ // g, function (m)
{var l = comments.length; comments.push (m); return '~~~ C' + l + '~~~'; })
.replace (/ ([^ \\]) \ / \ / [^ \ n] * \ n / g, function (m, f)
{var l = comments.length; comments.push (m); return f + '~~~ C' + l + '~~~'; })
// Remove regexp
.replace (/ \ / (\\\ / | [^ \ / \ n]) * \ / [gim] {0,3} / g, function (m)
{var l = res.length; res.push (m); return '~~~ R' + l + '~~~'; })
// Remove lines
.replace (/ ([^ \\]) ((?: '(?: \\' | [^ ']) *') | (?: "(?: \\" | [^ ") *" )) / g, function (m, f, s)
{var l = strings.length; strings.push (s); return f + '~~~ S' + l + '~~~'; })
// Highlight keywords
.replace (/ (var | function | typeof | new | return | if | for | in | while | break | do | continue | switch | case) ([^ a-z0-9 \ $ _]) / gi,
'<span class = "kwrd"> $ 1 </ span> $ 2')
// Select the brackets
.replace (/ (\ {| \} | \] | \ [| \ |) / gi,
'<span class = "gly"> $ 1 </ span>')
// Select the function names
.replace (/ ([az \ _ \ $] [a-z0-9 _] *) [\ s] * \ (/ gi,
'<span class = "func"> $ 1 </ span> (')
// Return the placements, strings, RegExp
.replace (/ ~~~ ([CSR]) (\ d +) ~~~ / g, function (m, t, i)
{return '<span class = "' + t + '">' + all [t] [i] + '</ span>'; })
// Expose Line Breaks
.replace (/ \ n / g,
'<br/>')
// Tabulation is replaced by non-breaking spaces
.replace (/ \ t / g,
'& nbsp; & nbsp; & nbsp; & nbsp;');
} Other styles will be needed here: .S {color: red} / * Strings are red * /
.func {color: blue} / * User functions blue * /
.C {color: orange} / * Comments orange * /
.kwrd {font-weight: bold} / * Keywords: bold * /
.R {color: gray} / * Gray regression * / Source: https://habr.com/ru/post/43030/
All Articles