Solved

ignore case in keyword during asp replace, yet maintain case

Posted on 2004-08-03
14
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 7

Expert Comment

by:j2nku
ID: 11709602
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
ID: 11709627
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
ID: 11709726
i'm timing out j2nku.  debugging right now....
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 63

Expert Comment

by:Zvonko
ID: 11710352
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
ID: 11710444
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
ID: 11710467
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
ID: 11710478
The first one was JavaScript, but the second one is VBScript.
0
 

Author Comment

by:pathlab
ID: 11710504
nevermind zvonko.  i'll try it tomorrow
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11710643
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
ID: 11710850
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
ID: 11713858
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
ID: 11719782
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
ID: 11720558
Thanks for points and feedback.
0
 

Author Comment

by:pathlab
ID: 11720615
welcome
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

626 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