Solved

ignore case in keyword during asp replace, yet maintain case

Posted on 2004-08-03
14
775 Views
Last Modified: 2010-07-27
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
Comment
Question by:pathlab
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 7

Expert Comment

by:j2nku
Comment Utility
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
 
LVL 7

Expert Comment

by:j2nku
Comment Utility
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
 

Author Comment

by:pathlab
Comment Utility
i'm timing out j2nku.  debugging right now....
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
For that are RegExp:

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

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

</script>

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
Comment Utility
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
 

Author Comment

by:pathlab
Comment Utility
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
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
The first one was JavaScript, but the second one is VBScript.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:pathlab
Comment Utility
nevermind zvonko.  i'll try it tomorrow
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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
 
LVL 2

Expert Comment

by:DejaVudew
Comment Utility
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
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
 

Author Comment

by:pathlab
Comment Utility
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
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Thanks for points and feedback.
0
 

Author Comment

by:pathlab
Comment Utility
welcome
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now