Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, so here's how I'm doing it:</p> <p>Using my original regex (with the added search pattern for the plain text, which happens to be any text that's left over after the tag searches are done):</p> <p><code>(?:(?:&lt;(?P&lt;tag&gt;script|style)[\s\S]*?&lt;/(?P=tag)&gt;)|(?:&lt;!--[\s\S]*?--&gt;)|(?:&lt;[\s\S]*?&gt;))|(?P&lt;text&gt;[^&lt;&gt;]*)</code></p> <p>Then in VB.Net:</p> <pre><code>Dim regexText As New Regex("(?:(?:&lt;(?&lt;tag&gt;script|style)[\s\S]*?&lt;/\k&lt;tag&gt;&gt;)|(?:&lt;!--[\s\S]*?--&gt;)|(?:&lt;[\s\S]*?&gt;))|(?&lt;text&gt;[^&lt;&gt;]*)", RegexOptions.IgnoreCase) Dim source As String = File.ReadAllText("html.txt") Dim evaluator As New MatchEvaluator(AddressOf MatchEvalFunction) Dim newHtml As String = regexText.Replace(source, evaluator) </code></pre> <p>The actual replacing of text happens here:</p> <pre><code>Private Function MatchEvalFunction(ByVal match As Match) As String Dim plainText As String = match.Groups("text").Value If plainText IsNot Nothing AndAlso plainText &lt;&gt; "" Then MatchEvalFunction = match.Value.Replace(plainText, plainText.Replace("Original word", "Replacement word")) Else MatchEvalFunction = match.Value End If End Function </code></pre> <p>Voila. <code>newHtml</code> now contains an exact copy of the original, except every occurrence of "Original word" in the page (as it's presented in a browser) is switched with "Replacement word", and all html and script code is preserved untouched. Of course, one could / would put in a more elaborate replacement routine, but this shows the basic principle. This is 12 lines of code, including function declaration and loading of html code etc. I'd be very interested in seeing a parallel solution, done in DOM etc for comparison (yes, I know this approach can be thrown off balance by <em>certain</em> occurrences of some nested tags quirks - in SCRIPT rewriting - but the damage from that will still be very limited, if any (see some of the comments above), and in general this will do the job pretty darn well).</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