Typically, this can be done simply using replace():
<% ' define the string to replace: strR = "ho" ' define the string which contains the strR: tStr = "fo bo ho yo wo fo ho fo zo" & _ " go bo ho fo ho ho yo to mo" ' replace the strR with <b>strR</b>: tStr = replace(tStr, strR, "<b>" & strR & "</b>") %> |
One problem with this solution is that it is case sensitive. So, if you were to have multiple upper/lowercase versions of the target word, it would only replace those that exactly match the case of your variable. The following example takes a slightly different approach, by looping through the string and inserting the highlighting tags (in this case <b></b>) at strategic points, including all case versions of the word.
<% ' define the string to replace: strR = "ho" ' define the string which contains the strR: tStr = "fo bo ho yo wo fo HO fo zo" & _ " go bo Ho fo hO ho yo to mo" response.write "Original:<p>" & tStr & "<p>" ' keep the length of strR handy: w = len(strR) ' loop through until strR is no longer in tStr: do while instr(lcase(tStr), lcase(strR)) > 0 ' get the current position of strR: cPos = instr(lcase(tStr), lcase(strR)) ' build the new string, maintaining case: nStr = nStr & _ left(tStr, cPos - 1) & _ "<b>" & mid(tStr, cPos, w) & "</b>" ' take the searched part out of tStr: tStr = right(tStr, len(tStr) - cPos - w + 1) loop ' append the leftover fragment to the new string: nStr = nStr & tStr response.write "New & improved:<P>" & nStr %> |
Betty asked, why not use the VBCompare ("ignore case") option of the replace command instead of miles of extra code?
Quite simply, you need to insert the tags at the specified position(s). While the replace command can certainly handle ignoring the case of the string, it will replace all instances of any case with the exact string in the third argument. See this example... the output is NOT the same as the input, since the upper case O's are no longer upper case:
<% response.Write replace("fOOd", "oo", "<b>oo</b>", 1, -1, 1) %> |
So, if it's all right that every instance of the string you're replacing is in one specific case structure, you can use this shortcut... but note that it may not be expected for the string to be altered that way.
Regular Expressions Chris Hohmann has provided both VBScript and JavaScript alternatives to the above code, using regular expressions:
VBScript:
<% Dim re, strInput, strOutput strInput = "" & _ "This a line of text containing the words one, two and three." & vbCRLF & _ "This line ends with the word two" & vbCRLF & _ "One is the word that starts this line." & vbCRLF & _ "This line contains the word cone." Set re = New RegExp re.Pattern="\b(one|two|three)\b" re.IgnoreCase=True re.Global=True strOutput=re.Replace(strInput,"<b>$1</b>") Response.Write("<pre>" & strOutput & "</pre>") %> |
JavaScript:
<script language="JavaScript" runat="SERVER"> var strInput = "" + "This a line of text containing the words one, two and three.\n" + "This line ends with the word two\n" + "One is the word that starts this line.\n" + "This line contains the word cone.\n"; Response.Write("<pre>" + strInput.replace(/\b(one|two|three)\b/gi,"<b>$1</b>") + "</pre>"); </script> |