www.xbdev.net
xbdev - software development
Thursday November 21, 2024
Home | Contact | Support | Programming.. More than just code .... | RegEx (Regular Expressions)... Powerful, expressive and flexible.. ..
     
 

RegEx (Regular Expressions)...

Powerful, expressive and flexible.. ..

 



Syntax Highlighting: Color Lets You See Things Differently




Imagine watching TV on a black and white TV? You can do it, but I bet you prefer color!  The same goes for coding - color synta...
Imagine watching TV on a black and white TV? You can do it, but I bet you prefer color! The same goes for coding - color syntax highlighting is a game changer when it comes to coding.



While there are lots of plugins and packages in lots of different languages, it's really easy to roll your own if you need to (thanks to regex).

You can write a powerful code syntax highlighter in less than 10 lines of code! Yes, really!


The limitations of the simple highlighter are its inability to handle errors - so if there is a missing comma or colon it will cause things to run over (no help or information).

Example


The following gives you a complete working example in Javascript; outputs the highlighted code as both raw and as a preview using some default css style values.


If you test out the following code example, this is what the output should look like. See links at the end for a working test v...
If you test out the following code example, this is what the output should look like. See links at the end for a working test version.




Things to Try


• Add syntax highlighting for comments (both single line and block)
• Try the simple highlighter with different programming languages
• Add additional highlighting expressions
• Try other styling values (e.g., bold, italic, underline, ...)
• Add extra highlighting for
warning
words/syntax/common errors
• Try porting the example code (which is in Javascript) to Python


Resources/Links


• Proof of Concept/Test Code [LINK]
• Other tutorials/demos [LINK]























 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.