using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class Test { public static void Main() { var searchTerm = "text"; var textToFormat = "This is some Text: "; var regex = new Regex($@"(<(?:img|a)(?:\s[^>]*>)?)|{Regex.Escape(searchTerm)}", RegexOptions.IgnoreCase); var highlightedText = regex.Replace(textToFormat, m => m.Groups[1].Success ? m.Groups[1].Value : $"{m.Value}"); Console.WriteLine(highlightedText); } }