/// <summary> /// Highlights one or more phrases everywhere in text by inserting it into a highlighter string. /// </summary> /// <param name="phrase">The phrase to highlight</param> /// <param name="highlighter">A string formatter where {0} will be replaced by the phrase (defaults to <strong class="highlighted">{0}</strong>)</param> public static string Highlight(this string text, string phrase, string highlighter="<strong class=\"highlighted\">{0}</strong>") { if (text.IndexOf(phrase, StringComparison.InvariantCultureIgnoreCase) == -1) return text; var replacer = new HighlightReplacer {Format = highlighter}; return Regex.Replace(text, phrase, replacer.OnPhraseMatch, RegexOptions.IgnoreCase); }
/// <summary> /// Highlights one or more phrases everywhere in text by inserting it into a highlighter string. /// </summary> /// <param name="phrase">The phrase to highlight</param> /// <param name="highlighter">A string formatter where {0} will be replaced by the phrase (defaults to <strong class="highlighted">{0}</strong>)</param> public static string Highlight(this string text, string phrase, string highlighter = "<strong class=\"highlighted\">{0}</strong>") { if (text.IndexOf(phrase, StringComparison.InvariantCultureIgnoreCase) == -1) { return(text); } var replacer = new HighlightReplacer { Format = highlighter }; return(Regex.Replace(text, phrase, replacer.OnPhraseMatch, RegexOptions.IgnoreCase)); }