Solved

ignore case in keyword during asp replace, yet maintain case

Posted on 2004-08-03
14
781 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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