EDITED: if you want to extend the code to allow properties, the modification is very simple. Just check if the regualr expression contains a space or not. If so, look in the allowed array for a match without any changes in properties, values, etc. The code is changed and the image is added.
I know that you asked for a solution to your approach, but I recommend a different approach to what you want to accomplish.
You can easily overcome this problem if you use Regular Expression.
The idea is simple. In the RichTextBox_TextChanged event, the regular expression creator iterates over the entire text and looks for any HTML tag (one that starts with < and ends with > ), regardless of the text in between.
Then, instead of iterating over all the valid HTML tags in your array, one simple line can easily determine if the Contains array is an element or not.


Here is my (tested and working) code.
Imports System.Text.RegularExpressions Public Class Form1 Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RichTextBox1.TextChanged Dim current_cursor_position As Integer = Me.RichTextBox1.SelectionStart 'This is useful to get a hold of where is the current cursor at 'this will be needed once all coloring is done, and we need to return Dim html() As String = {"<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>", "<body>", "</body>", "pre>", "</pre>", "<!DOCTYPE>", "<title>", "</title>", "<a>", "<abbr>", "<address>", "<area>", "<article>", "<aside>", "<audio>", "<acronym>", "<applet>", "<b>", "<base>", "<bdi>", "<bdo>", "<blockquote>", "<body>", "<br>", "<button>", "<basefont>", "<bgsound>", "<big>", "<blink>", "<img>","</img>", "<input>","</input>"} Dim pattern As String = "<(.)*?>" Dim matches As MatchCollection = Regex.Matches(Me.RichTextBox1.Text, pattern) For Each match In matches Me.RichTextBox1.Select(match.index, match.length) Dim lookFor As String = match.ToString If match.ToString.Contains(" ") Then 'Checking if tag contains properties lookFor = match.ToString.Substring(0, match.ToString.IndexOf(" ")) & ">" 'This line will strip away any extra properties, values, and will ' close up the tag to be able to look for it in the allowed array End If If html.Contains(lookFor.ToString.ToLower) Then 'The tag is part of the allowed tags, and can be colored green. Me.RichTextBox1.SelectionColor = Color.Green Else 'This tag is not recognized, and shall be colored black.. Me.RichTextBox1.SelectionColor = Color.Black End If Next Me.RichTextBox1.SelectionStart = current_cursor_position 'Returning cursor to its original position Me.RichTextBox1.SelectionLength = 0 'De-Selecting text (if it was selected) Me.RichTextBox1.SelectionColor = Color.Black 'new text will be colored black, until 'recognized as HTML tag. End Sub End Class
PS: you can also not expand the array of valid html elements by simply using a regular expression to search for valid HTML tags (with the flexibility of spaces between tags, properties and values, etc.
If you want, I could elaborate on this.