Syntax Highlighter has become the trivial part of every programmers life. Since “Oxford English Dictionary” which was published in the year 1985, we are addicted to these editors. In this post, I will walk you through a very simple and basic syntax highlighter application.

Our first goal is define a contract / type which will hold the details of our need. So let me create a type called HighlightInfo as below.

Each and every object of this type will represent single highlight setting. The color possess two properties for itself to avoid the issue that the Color is not serializable (For more info on the same do read the post “Workaround For Non Serializable Types“). The “Words” will be a comma seperated words to be highlighted. The list of such above mentioned type will encapsulate the entire settings.

When it comes to algorithms, you should be very careful about the performance since it might involve a massive data which will be involved in the process. One such algorithm can be searching for multiple strings on a single statement using regular expression. Check out “Search Multiple Strings Using Regular Expression“. Also in our sample, we make the highlighting on every text change for the entire text which should be converted to use only the changed text when it comes to huge text.

If you are looking for building a web application with this feature then ASP.net + Javascript or Javascript alone will be your better option. Since Javascript also supports regular expressions, you might not need to change most of the code.

Do note that this sample will serve as a guide for building your syntax highlighter and the logic and design might need the change as per the requirement.