include the following resources inside the <head>
tag.
<link href="highlightJavascript.css" rel="stylesheet" />
<script src="highlightJavascript.min.js"></script>
And just before the closing of the <body>
tag.
<script>highlightJavascript.format();</script>
highlight code with <pre><code class="js"><script>...</script></code></pre>
or <pre><code class="js"><!--...--></code></pre>
highlightJavascript includes javascript language highlighting built right in, but you can view the language file at language/javascript.js to build your own.
Currently the only option available is highlightJavascript.format("nolines");
which as the name implies, disables line numbering.
highlightJavascript looks for certain RegExp when parsing code such as comments, strings and regexp, as these need a higher priority than other regex.
Unless otherwise specified, the following will resort to javascript syntax when not otherwise defined.
"escaped" is used to detect escaped characters in strings and regular expressions. (optional, will not style if undefined) JavaScript example: /\(?:0[0-3][0-7][0-7]|[0-3][0-7][0-7]|[0-7][0-7]|[0-9]|.)/g
"string" is used to detect single and multiline strings. defaults to: /(?:'[^'\\](?:\.[^'\\])')|(?:"[^"\\](?:\.[^"\\])")/g
"singleComment" is used to detect single line comments. Called before regex. defaults to: ///.+?(?=\n|\r|$)/ig
"multiComment" is used to detect multiline comments. Called after regex. defaults to: //*[\s\S]+?*//g
RegExp is run in two parts. The first detects regex without a string, the second detects the remaining regex. This prevents some interesting errors like /^\s+|\s+$/g, '').replace(/\s+/g
being styled.
"regex1" defaults to: //(?![*])(?:[`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?a-z0-9])+/[gim]*(?=,|;|]|)|}|\n|\r|\n\r|$)(?![a-z0-9\040])/gi
"regex2" defaults to: //(?![*])(?:\040*[`~!@#$%^&*()-_=+[{]}\|;:'",<.>/?a-z0-9])+/[gim]*(?=,|;|]|)|}|\n|\r|\n\r)(?![a-z0-9\040])/gi
"link" is used to detect html links: <a href="" >link</a>
. defaults to: /<a\b href="[htpfs]+://[^"]+"[^>]>(?:.?)</a>/g
"url" is used to convert url's to html links. defaults to: /(?:http|ftp|https)://[\w-]+(?:.[\w-]+)+(?:[\w-.,@?^=%&:/
+#]*[\w-@?^=%&/+#])?/g
All other syntax is defined from highest to lowest priority in a language file and will run after links, comments, regex, strings, and escaped characters.
-
in rare instances the space character in a regular expression, or two regular expressions on the same line will cause parsing issues.
-
Compressed code does not format well, as regular expression parsing works best when there are line breaks often. Another syntax highlighter may suit your needs better if you plan on displaying compressed code.