Encapsulates found regexp, strings, comments, keywords, predefined objects, numbers, brackets and operators into t-tags with a matching class.
Requires a RegExp to match regexp, strings, comments, keywords, predefined objects, numbers, brackets and operators, e.g.:
var re = /(?![\d\w]\s*)(\/[^\/\*][^\n\/]*\/[gi])|(".*?"|'.*?')|(\/\/.*?\n|\/\*[\x00-\xff\u00\uffff]*?\*\/)|(?:\b)(abstract|boolean|break|byte|case|catch|char|class|const|continue|debugger|default|delete|do|double|else|enum|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with)(?:\b)|(?:\b)(Array|Boolean|Date|Function|Math|Number|Object|RegExp|String|document|window|arguments)(?:\b)|(\d[\d\.eE]*)|([\x28-\x2b\x2d\x3a-\x3f\x5b\x5d\x5e\x7b-\x7e]+|\x2f|(?=\D)\.(?=\D))/g;
Provides a filter inserting t-tags* with the following classNames:
- f-1 = regexp
- f1 = string
- f2 = comment
- f3 = keyword
- f4 = predefined object
- f5 = number
- f6 = operator, bracket
remember to use innerText/firstChild.data instead of innerHTML to avoid its ability to convert HTML entities which cannot be matched here. "&" needs to be escaped beforehand, otherwise will be transformed on html reinsertion.
* IE6-8 need a js shim to allow for the non-standard tag:
document.createElement('t');
This was created with the 140byt.es homepage in mind, too :-)
Is this used on the 140byt.es main page? Some of the code snippets there are not highlighted well. Let's see if we can fix this.
".*?"to"(?:\\.|\\\r*\n|[^\n"])*"and'.*?'to'(?:\\.|\\\r*\n|[^\n'])*'. This also matches escaped line breaks. I'm not sure if this is allowed in JavaScript, but it makes the syntax highlighting more reliable.(?![\d\w]\s*)(\/[^\/\*][^\n\/]*\/[gi])to(\/(?:\\.|[^\n/*])(?:\\.|[^\n/])*\/[gim]*)(according to this ECMAScript specification). This also fixes some other issues like not allowing/.../gi. The part(?!...)is noneffective and can be omitted, as explained below.\/\/.*?\nto\/\/[^\n]*\n, this is faster.(?:\b)to\b, the brackets are not needed.(?=\D)\.(?=\D)is a bit strange since it uses two zero-width positive lookaheads. The first(?=\D)looks for a non-digit but does not advance the position. Then\.checks if the same character (which we know is a non-digit) is a dot. Due to this the first bracket is noneffective and can be omitted. Same with the negative lookahead above. It checks if the trailing slash of the regular expression is not a digit or word character. This is always true for a slash. A lookbehind (using(?<=...)) would be appropriate but is not supported in JavaScript unfortunately.