document body minHeight 600px function simplehighlighter str str str replace bnull b b 0 9 b btrue b bfalse b g span class text 1 span str str replace g span class bracket span str str replace a z w gi span class variable span str str replace and or g span class operator span str str replace b for function return unset count global if else else if intval int array b g span class command 1 span return str Test the simple highlighter let a Simple test code int i 0 for let b 0 b 10 b if b 0 return let r simplehighlighter a view the raw highlighted code console log r preview output let styles style text color green bracket color gray variable color blue operator color red command color orange style let div document createElement div document body appendChild div div style width 100 div innerHTML styles r style body min height 400px codeblock white space pre font family Courier text color green bracket color gray variable color blue operator color red command color orange style script function simplehighlighter str str str replace bnull b b 0 9 b btrue b bfalse b g span class text 1 span str str replace g span class bracket span str str replace a z w gi span class variable span str str replace and or g span class operator span str str replace b for function return unset count global if else else if intval int array b g span class command 1 span return str Test the simple highlighter let a Simple test code int i 0 for let b 0 b 10 b if b 0 return let r simplehighlighter a view the raw highlighted code console log r preview output let div document createElement div document body appendChild div div className codeblock div style width 100 div innerHTML r script Add extra lines for comments So comments don t mix with others they re removed style body min height 400px codeblock white space pre font family Courier text color green font weight 900 bracket color gray variable color blue operator color red command color orange comment color gray style script Todo add in reserve and keyword customizations const reserved abstract boolean byte char double final float goto int long native short synchronized throws transient volatile const keywords abstract arguments await boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let 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 yield function simplehighlighter str const multicomments str matchAll g str str replace g XOMULTICODEOX const singlecomments str matchAll n r n r g str str replace n r n r g XOSINGLECODEOX const stringblocks str matchAll bnull b b 0 9 b btrue b bfalse b g str str replace bnull b b 0 9 b btrue b bfalse b g XOTEXTCODEOX str str replace g span class bracket span str str replace a z w gi span class variable span str str replace and or g span class operator span str str replace b for function return unset count global if else else if intval int array b g span class command 1 span let codeindex 0 str str replace XOMULTICODEOX g function match token return span class comment multicomments codeindex 0 span codeindex 0 str str replace XOSINGLECODEOX g function match token return span class comment singlecomments codeindex 0 span codeindex 0 str str replace XOTEXTCODEOX g function match token return span class text stringblocks codeindex 0 span return str Test the simple highlighter let a Simple test code int i 0 let v for int i 2 for let b 0 b 10 b if b 0 a comment return let a a string let b another string test let a 2 cats milk let b 2 test let r simplehighlighter a view the raw highlighted code console log r preview output let div document createElement div document body appendChild div div className codeblock div style width 100 div innerHTML r console log ready script
double final float goto int long native short synchronized throws transient volatile const keywords abstract arguments await boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let 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 yield function simplehighlighter str const multicomments str matchAll g str str replace g XOMULTICODEOX const singlecomments str matchAll n r n r g str str replace n r n r g XOSINGLECODEOX const stringblocks str matchAll bnull b b 0 9 b btrue b bfalse b g str str replace bnull b b 0 9 b btrue b bfalse b g XOTEXTCODEOX str str replace g span class bracket span str str replace a z w gi span class variable span str str replace and or g span class operator span str str replace b for function return unset count global if else else if intval int array b g span class command 1 span let codeindex 0 str str replace XOMULTICODEOX g function match token return span class comment multicomments codeindex 0 span codeindex 0 str str replace XOSINGLECODEOX g function match token return span class comment singlecomments codeindex 0 span codeindex 0 str str replace XOTEXTCODEOX g function match token return span class text stringblocks codeindex 0 span return str Test the simple highlighter let a Simple test code int i 0 let v for int i 2 for let b 0 b 10 b if b 0 a comment return let a a string let b another string test let a 2 cats milk let b 2 test let r simplehighlighter a view the raw highlighted code console log r preview output let div document createElement div document body appendChild div div className codeblock div style width 100 div innerHTML r console log ready script