fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var searchTerm = "text";
  12. var textToFormat = "This is some Text: <a href='/text.html'><img src='/images/text.png'/></a>";
  13. var regex = new Regex($@"(<(?:img|a)(?:\s[^>]*>)?)|{Regex.Escape(searchTerm)}", RegexOptions.IgnoreCase);
  14.  
  15. var highlightedText = regex.Replace(textToFormat, m =>
  16. m.Groups[1].Success ? m.Groups[1].Value : $"<span class='highlight'>{m.Value}</span>");
  17. Console.WriteLine(highlightedText);
  18. }
  19. }
Success #stdin #stdout 0.06s 21112KB
stdin
Standard input is empty
stdout
This is some <span class='highlight'>Text</span>: <a href='/text.html'><img src='/images/text.png'/></a>