Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

ignore case in keyword during asp replace, yet maintain case

my question is similar to this one:

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20671946.html?query=asp+%22ignore+case%22+replace&clearTAFilter=true

i want to replace a keyword in a text with "<b>"&keyword&"<\b>", but i want to ignore case in searching, but i want to maintain case across the replace.  for example, given a keyword or "REPLACE" or "rEpLAce" or any case insensitive version of "replace"...

i want to replace the text:"REPLACE, rePLACE, Replace!!!!!!!"
with the text: "<b>REPLACE</b>,<b>rePLACE</b>,<b>Replace</b>!!!!!!!"

i'm all out of ideas.  help!??!

pat
0
pathlab
Asked:
pathlab
  • 5
  • 5
  • 2
  • +2
1 Solution
 
j2nkuCommented:
ok, text is in variable MyText, here's the code:

searchword = "replace"
MyText = "RepLACE, REplACe,..."
start = 1
swlen = len(searchword) ' we are going to use it
atend = false
do while not atend
  wstart = instr(start, MyText, searchword, 1)
  if wstart > 0 Then
    MyText = left(MyText,wstart) & "<b>" & mid(MyText,wstart, swlen) & "</b>" & right(MyText,len(MyText) - wstart + swlen)
  else
  atend = true
  end if
loop


It should do the right replacing, but it's untested so some values might be wrong by one letter.
0
 
j2nkuCommented:
Of course, forgot few things - updated code:

searchword = "replace"
MyText = "RepLACE, REplACe,..."
start = 1
swlen = len(searchword) ' we are going to use it
atend = false
do while not atend
  wstart = instr(start, MyText, searchword, 1)
  if wstart > 0 Then
    MyText = left(MyText,wstart) & "<b>" & mid(MyText,wstart, swlen) & "</b>" & right(MyText,len(MyText) - wstart + swlen)
    start = wstart + swlen + 7 ' we're adding all the  bold things also!
  else
  atend = true
  end if
loop
0
 
pathlabAuthor Commented:
i'm timing out j2nku.  debugging right now....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZvonkoSystems architectCommented:
For that are RegExp:

<script>
myText = "REPLACE, rePLACE, Replace!!!!!!!"

alert(myText.replace(/(replace)/gi,"<b>$1</b>"));

</script>

0
 
ZvonkoSystems architectCommented:
Sorry, wrong topic area :)
Take better this one:

<%

Set objRegExp = New RegExp
objRegExp.Pattern = "(replace)"
objRegExp.Global = True
objRegExp.IgnoreCase = True

myText = "REPLACE, rePLACE, Replace!!!!!!!"

Response.Write(objRegExp.Replace(myText, "<b>$1</b>"))

%>


0
 
pathlabAuthor Commented:
j2nku - still getting timeout errors,  and i haven't had time to check where it's looping forever
zvonko - that's vbscript or javascript?
0
 
ZvonkoSystems architectCommented:
The first one was JavaScript, but the second one is VBScript.
0
 
pathlabAuthor Commented:
nevermind zvonko.  i'll try it tomorrow
0
 
Carl TawnSystems and Integration DeveloperCommented:
Try this as an example:

    myString = "Replace, REPLACE, RePlAcE"

    iStart = InStr(1, myString, "replace", 1)

    While iStart > 0

        tmp = Mid(myString, iStart, 7)
        myString = Replace(myString, tmp, "<b>" & tmp & "</b>")
        iStart = InStr(iStart + 7, myString, "replace", 1)

    Wend

Hope it helps
0
 
DejaVudewCommented:
I'd go with zvonko's solution. How large is the text you're searching through that's giving you the timeout problem? You might want to use his javascript solution if it's a particularly large file -- which would work similarly to how this site underlines particular pieces of text for advertisements.
0
 
ZvonkoSystems architectCommented:
Here is the RegExp version as a Function:

<%
Function insertTags(theStr, searchStr, startTag, endTag)
  Set objRegExp = New RegExp
  objRegExp.Pattern = "("&searchStr&")"
  objRegExp.Global = True
  objRegExp.IgnoreCase = True
  insertTags = objRegExp.Replace(theStr, startTag & "$1" & endTag)
End Function

myText = "REPLACE, rePLACE, Replace!!!!!!!"

Response.Write(insertTags(myText, "replace", "<b>", "</b>"))

%>


0
 
pathlabAuthor Commented:
thanks for the input.  here's what i finally did.  

<%
'boldata returns a string
'bolds all instances of a searchword in text and highlights them with a specified color (think google cached pages)
'case insensitive, for color, use strings like: "#A0FFFF", "99ff99", "#ffff66", "#ff9999"

Function bolddata (text,searchword, color)
      Dim objRegExp

      Set objRegExp = New RegExp
      objRegExp.Pattern = "("&searchword&")"
      objRegExp.Global = True
      objRegExp.IgnoreCase = True
      bolddata = objRegExp.Replace(text, "<b style=""color:black;background-color:"&color&""">$1</b>")
end function
%>
0
 
ZvonkoSystems architectCommented:
Thanks for points and feedback.
0
 
pathlabAuthor Commented:
welcome
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now