Solved

ignore case in keyword during asp replace, yet maintain case

Posted on 2004-08-03
14
778 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…

860 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