Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to highlight a string within a string ignoring whitespace and non alphanumeric chars?
    text
    copied!<p>What is the best way to produce a highlighted string found within another string?</p> <p>I want to ignore all character that are not alphanumeric but retain them in the final output.</p> <p>So for example a search for 'PC3000' in the following 3 strings would give the following results:</p> <pre><code> ZxPc 3000L = Zx&lt;font color='red'&gt;Pc 3000&lt;/font&gt;L ZXP-C300-0Y = ZX&lt;font color='red'&gt;P-C300-0&lt;/font&gt;Y Pc3 000 = &lt;font color='red'&gt;Pc3 000&lt;/font&gt; </code></pre> <p>I have the following code but the only way i can highlight the search within the result is to remove all the whitespace and non alphanumeric characters and then set both strings to lowercase. I'm stuck!</p> <pre><code>public string Highlight(string Search_Str, string InputTxt) { // Setup the regular expression and add the Or operator. Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase); // Highlight keywords by calling the delegate each time a keyword is found. string Lightup = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords)); if (Lightup == InputTxt) { Regex RegExp2 = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase); RegExp2.Replace(" ", ""); Lightup = RegExp2.Replace(InputTxt.Replace(" ", ""), new MatchEvaluator(ReplaceKeyWords)); int Found = Lightup.IndexOf("&lt;font color='red'&gt;"); if (Found == -1) { Lightup = InputTxt; } } RegExp = null; return Lightup; } public string ReplaceKeyWords(Match m) { return "&lt;font color='red'&gt;" + m.Value + "&lt;/font&gt;"; } </code></pre> <p>Thanks guys!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload