Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Highlight Search Word in Results

Reply
Thread Tools

Highlight Search Word in Results

 
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
Hello

I have a little function to highlight text if it exists.

Function Highlight(vFind, vSearch)
Dim RegEx
Set RegEx = New RegExp
RegEx.Pattern = vFind
RegEx.IgnoreCase = True
Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
"</span>")
Set RegEx = Nothing
End Function

The only problem is, that if I search for "something", "something" appears
highlighted when the string being searched actually contained "Something".
This is not about case sensitive searching, this is about showing the actual
match rather than what was being searched for. Hope that makes sense.

For example:

Response.Write Highlight("something", "Have you seen Something About
Mary?")

Gives:

Have you seen <span class="Highlight">something</span> About Mary?

Whilst this is ok, it would be great if it actually highlighted the match
rather than the 'find, i.e:

Have you seen <span class="Highlight">Something</span> About Mary?

I have looked in to the documentation a bit and it seems that to do what I
want involves using the execute method and then hunting through the matches
collection. Does anyone have any sample code for this that I could
_borrow_?

My function is nice and small and it would be a shame to have to go for
something bigger, (a.k.a. performance degradation), if a simple change can
be made to my function.

I am open to suggestions on the JScript approach also as I notice these
special characters $0 - $9 although MSDN is somewhat lacking in sample code
for a novice like me.

MTIA

David


 
Reply With Quote
 
 
 
 
Steven Burn
Guest
Posts: n/a
 
      08-27-2004
Function Highlight(sFind, sSearch)
Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" & sFind
& "</span>")
End Function

--

Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!


"David Morgan" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Hello
>
> I have a little function to highlight text if it exists.
>
> Function Highlight(vFind, vSearch)
> Dim RegEx
> Set RegEx = New RegExp
> RegEx.Pattern = vFind
> RegEx.IgnoreCase = True
> Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
> "</span>")
> Set RegEx = Nothing
> End Function
>
> The only problem is, that if I search for "something", "something" appears
> highlighted when the string being searched actually contained "Something".
> This is not about case sensitive searching, this is about showing the

actual
> match rather than what was being searched for. Hope that makes sense.
>
> For example:
>
> Response.Write Highlight("something", "Have you seen Something About
> Mary?")
>
> Gives:
>
> Have you seen <span class="Highlight">something</span> About Mary?
>
> Whilst this is ok, it would be great if it actually highlighted the match
> rather than the 'find, i.e:
>
> Have you seen <span class="Highlight">Something</span> About Mary?
>
> I have looked in to the documentation a bit and it seems that to do what I
> want involves using the execute method and then hunting through the

matches
> collection. Does anyone have any sample code for this that I could
> _borrow_?
>
> My function is nice and small and it would be a shame to have to go for
> something bigger, (a.k.a. performance degradation), if a simple change can
> be made to my function.
>
> I am open to suggestions on the JScript approach also as I notice these
> special characters $0 - $9 although MSDN is somewhat lacking in sample

code
> for a novice like me.
>
> MTIA
>
> David
>
>



 
Reply With Quote
 
 
 
 
Chris Hohmann
Guest
Posts: n/a
 
      08-27-2004
"David Morgan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello
>
> I have a little function to highlight text if it exists.
>
> Function Highlight(vFind, vSearch)
> Dim RegEx
> Set RegEx = New RegExp
> RegEx.Pattern = vFind
> RegEx.IgnoreCase = True
> Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
> "</span>")
> Set RegEx = Nothing
> End Function
>
> The only problem is, that if I search for "something", "something" appears
> highlighted when the string being searched actually contained "Something".
> This is not about case sensitive searching, this is about showing the

actual
> match rather than what was being searched for. Hope that makes sense.
>
> For example:
>
> Response.Write Highlight("something", "Have you seen Something About
> Mary?")
>
> Gives:
>
> Have you seen <span class="Highlight">something</span> About Mary?
>
> Whilst this is ok, it would be great if it actually highlighted the match
> rather than the 'find, i.e:
>
> Have you seen <span class="Highlight">Something</span> About Mary?
>
> I have looked in to the documentation a bit and it seems that to do what I
> want involves using the execute method and then hunting through the

matches
> collection. Does anyone have any sample code for this that I could
> _borrow_?
>
> My function is nice and small and it would be a shame to have to go for
> something bigger, (a.k.a. performance degradation), if a simple change can
> be made to my function.
>
> I am open to suggestions on the JScript approach also as I notice these
> special characters $0 - $9 although MSDN is somewhat lacking in sample

code
> for a novice like me.


http://aspfaq.com/show.asp?id=2344


 
Reply With Quote
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
Hi Curt

Thanks for your suggestion. I may end up falling back to it, but I was
really trying to avoid string concatenation and looping as per my
penultimate sentence.

Regards

David


"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ePdw$K%(E-Mail Removed)...
> http://www.darkfalz.com/1089
>
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> Site: http://www.Darkfalz.com
> Blog: http://blog.Darkfalz.com
>
>
> "David Morgan" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hello
> >
> > I have a little function to highlight text if it exists.
> >
> > Function Highlight(vFind, vSearch)
> > Dim RegEx
> > Set RegEx = New RegExp
> > RegEx.Pattern = vFind
> > RegEx.IgnoreCase = True
> > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

&
> > "</span>")
> > Set RegEx = Nothing
> > End Function
> >
> > The only problem is, that if I search for "something", "something"

appears
> > highlighted when the string being searched actually contained

"Something".
> > This is not about case sensitive searching, this is about showing the
> > actual
> > match rather than what was being searched for. Hope that makes sense.
> >
> > For example:
> >
> > Response.Write Highlight("something", "Have you seen Something About
> > Mary?")
> >
> > Gives:
> >
> > Have you seen <span class="Highlight">something</span> About Mary?
> >
> > Whilst this is ok, it would be great if it actually highlighted the

match
> > rather than the 'find, i.e:
> >
> > Have you seen <span class="Highlight">Something</span> About Mary?
> >
> > I have looked in to the documentation a bit and it seems that to do what

I
> > want involves using the execute method and then hunting through the
> > matches
> > collection. Does anyone have any sample code for this that I could
> > _borrow_?
> >
> > My function is nice and small and it would be a shame to have to go for
> > something bigger, (a.k.a. performance degradation), if a simple change

can
> > be made to my function.
> >
> > I am open to suggestions on the JScript approach also as I notice these
> > special characters $0 - $9 although MSDN is somewhat lacking in sample
> > code
> > for a novice like me.
> >
> > MTIA
> >
> > David
> >
> >

>
>



 
Reply With Quote
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
Err... thanks, but you have missed my point.

"Steven Burn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Function Highlight(sFind, sSearch)
> Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

sFind
> & "</span>")
> End Function
>
> --
>
> Regards
>
> Steven Burn
> Ur I.T. Mate Group
> www.it-mate.co.uk
>
> Keeping it FREE!
>
>
> "David Morgan" <(E-Mail Removed)> wrote in message
> news:#(E-Mail Removed)...
> > Hello
> >
> > I have a little function to highlight text if it exists.
> >
> > Function Highlight(vFind, vSearch)
> > Dim RegEx
> > Set RegEx = New RegExp
> > RegEx.Pattern = vFind
> > RegEx.IgnoreCase = True
> > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

&
> > "</span>")
> > Set RegEx = Nothing
> > End Function
> >
> > The only problem is, that if I search for "something", "something"

appears
> > highlighted when the string being searched actually contained

"Something".
> > This is not about case sensitive searching, this is about showing the

> actual
> > match rather than what was being searched for. Hope that makes sense.
> >
> > For example:
> >
> > Response.Write Highlight("something", "Have you seen Something About
> > Mary?")
> >
> > Gives:
> >
> > Have you seen <span class="Highlight">something</span> About Mary?
> >
> > Whilst this is ok, it would be great if it actually highlighted the

match
> > rather than the 'find, i.e:
> >
> > Have you seen <span class="Highlight">Something</span> About Mary?
> >
> > I have looked in to the documentation a bit and it seems that to do what

I
> > want involves using the execute method and then hunting through the

> matches
> > collection. Does anyone have any sample code for this that I could
> > _borrow_?
> >
> > My function is nice and small and it would be a shame to have to go for
> > something bigger, (a.k.a. performance degradation), if a simple change

can
> > be made to my function.
> >
> > I am open to suggestions on the JScript approach also as I notice these
> > special characters $0 - $9 although MSDN is somewhat lacking in sample

> code
> > for a novice like me.
> >
> > MTIA
> >
> > David
> >
> >

>
>



 
Reply With Quote
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
Thanks Chris

When I adapt your VBScript example from ASP FAQ, which is almost identical
to mine, I get $1 instead of the match in the correct case.

If you read the documentation regarding regular expressions you will see:

(pattern) Matches pattern and captures the match. The captured match
can be retrieved from the resulting Matches collection, using the SubMatches
collection in VBScript or the $0.$9 properties in JScript. To match
parentheses characters ( ), use '\(' or '\)'.



In other words, the $0 - $9 properties are not available in VBScript. Have
I missed something here or has Aaron included something on the FAQ without
testing it!

With a little bit of modification I have managed to convert your Javascript
example in to this, which does the job, (and matches whole words only,
unlike my VBScript version).

function jsHighlight(vFind, vSearch) {
var re = new RegExp('\\b(' + vFind + ')\\b', 'gi')
return vSearch.replace(re, '\$1')
}

Thanks and regards

David


"Chris Hohmann" <(E-Mail Removed)> wrote in message
news:eOh69$%(E-Mail Removed)...
> "David Morgan" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hello
> >
> > I have a little function to highlight text if it exists.
> >
> > Function Highlight(vFind, vSearch)
> > Dim RegEx
> > Set RegEx = New RegExp
> > RegEx.Pattern = vFind
> > RegEx.IgnoreCase = True
> > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

&
> > "</span>")
> > Set RegEx = Nothing
> > End Function
> >
> > The only problem is, that if I search for "something", "something"

appears
> > highlighted when the string being searched actually contained

"Something".
> > This is not about case sensitive searching, this is about showing the

> actual
> > match rather than what was being searched for. Hope that makes sense.
> >
> > For example:
> >
> > Response.Write Highlight("something", "Have you seen Something About
> > Mary?")
> >
> > Gives:
> >
> > Have you seen <span class="Highlight">something</span> About Mary?
> >
> > Whilst this is ok, it would be great if it actually highlighted the

match
> > rather than the 'find, i.e:
> >
> > Have you seen <span class="Highlight">Something</span> About Mary?
> >
> > I have looked in to the documentation a bit and it seems that to do what

I
> > want involves using the execute method and then hunting through the

> matches
> > collection. Does anyone have any sample code for this that I could
> > _borrow_?
> >
> > My function is nice and small and it would be a shame to have to go for
> > something bigger, (a.k.a. performance degradation), if a simple change

can
> > be made to my function.
> >
> > I am open to suggestions on the JScript approach also as I notice these
> > special characters $0 - $9 although MSDN is somewhat lacking in sample

> code
> > for a novice like me.

>
> http://aspfaq.com/show.asp?id=2344
>
>



 
Reply With Quote
 
Aaron [SQL Server MVP]
Guest
Posts: n/a
 
      08-27-2004
> In other words, the $0 - $9 properties are not available in VBScript.
Have
> I missed something here or has Aaron included something on the FAQ without
> testing it!


??? What are you talking about?

The 'untested' sample you mention works perfectly fine here, both in JScript
and in VBScript. Maybe the problem is in your "adaptation" of the code...

--
http://www.aspfaq.com/
(Reverse address to reply.)


 
Reply With Quote
 
Steven Burn
Guest
Posts: n/a
 
      08-27-2004
Not really....... you wanted to know why "Something" was treated as
"something"..??

Although I know you can, I can't see any point in using RegExp for something
that can be done with the native Replace() function in one line of code.

What your RegExp is doing with "RegExp.IgnoreCase = True" is essentially the
same as converting the query to lowercase before replacing the word/phrase

--

Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!


"David Morgan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Err... thanks, but you have missed my point.
>
> "Steven Burn" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Function Highlight(sFind, sSearch)
> > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

> sFind
> > & "</span>")
> > End Function
> >
> > --
> >
> > Regards
> >
> > Steven Burn
> > Ur I.T. Mate Group
> > www.it-mate.co.uk
> >
> > Keeping it FREE!
> >
> >
> > "David Morgan" <(E-Mail Removed)> wrote in message
> > news:#(E-Mail Removed)...
> > > Hello
> > >
> > > I have a little function to highlight text if it exists.
> > >
> > > Function Highlight(vFind, vSearch)
> > > Dim RegEx
> > > Set RegEx = New RegExp
> > > RegEx.Pattern = vFind
> > > RegEx.IgnoreCase = True
> > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" &

vFind
> &
> > > "</span>")
> > > Set RegEx = Nothing
> > > End Function
> > >
> > > The only problem is, that if I search for "something", "something"

> appears
> > > highlighted when the string being searched actually contained

> "Something".
> > > This is not about case sensitive searching, this is about showing the

> > actual
> > > match rather than what was being searched for. Hope that makes sense.
> > >
> > > For example:
> > >
> > > Response.Write Highlight("something", "Have you seen Something About
> > > Mary?")
> > >
> > > Gives:
> > >
> > > Have you seen <span class="Highlight">something</span> About Mary?
> > >
> > > Whilst this is ok, it would be great if it actually highlighted the

> match
> > > rather than the 'find, i.e:
> > >
> > > Have you seen <span class="Highlight">Something</span> About Mary?
> > >
> > > I have looked in to the documentation a bit and it seems that to do

what
> I
> > > want involves using the execute method and then hunting through the

> > matches
> > > collection. Does anyone have any sample code for this that I could
> > > _borrow_?
> > >
> > > My function is nice and small and it would be a shame to have to go

for
> > > something bigger, (a.k.a. performance degradation), if a simple change

> can
> > > be made to my function.
> > >
> > > I am open to suggestions on the JScript approach also as I notice

these
> > > special characters $0 - $9 although MSDN is somewhat lacking in sample

> > code
> > > for a novice like me.
> > >
> > > MTIA
> > >
> > > David
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
Hi Aaron

I believed that I had caveated that sentence enough so as not to insult. It
would seem not. You will note it starts with "Have I missed something
here". Please accept my apologies for your inference.

The sentence you have cited below is a question, not a statement, excuse my
poor grammar and the absence of a question mark.

I was not surprised by my VBScript code not working as MSDN says $0-$9 is
for JScript. I am now wholly confused as to why Chris' example _is_working!
Maybe I'm on an old version of the VBScript docs or something.

Here is my VBScript function adapted based on Chris' example:

Function Highlight(vFind, vSearch)
Dim RegEx
Set RegEx = New RegExp
RegEx.Pattern = "\b" & vFind & "\b"
RegEx.IgnoreCase = True
RegEx.Global = True
Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">$1</span>")
Set RegEx = Nothing
End Function

When I use the above, I get $1 literally returned, not the match like in
Chris' example, (included at the end), and I'm confused as to why.

If anyone has the time they could paste the whole thing below into an new
ASP and see what they think.

Thanks

David M

<% Dim re, strInput, strOutput
strInput = "" & _
"This a line of text containing the words one, two and three." &
vbCRLF & _
"This line ends with the word two" & vbCRLF & _
"One is the word that starts this line." & vbCRLF & _
"This line contains the word cone."

Set re = New RegExp
re.Pattern="\b(" & "One" & ")\b"
re.IgnoreCase=True
re.Global=True
strOutput=re.Replace(strInput,"<b>$1</b>")
Response.Write("<pre>" & strOutput & "</pre>")
Response.Write "<hr>"
Response.Write Highlight("One", strInput)%>
<SCRIPT LANGUAGE=vbscript RUNAT=Server>
Function Highlight(vFind, vSearch)
Dim RegEx
Set RegEx = New RegExp
RegEx.Pattern = "\b" & vFind & "\b"
RegEx.IgnoreCase = True
RegEx.Global = True
Highlight = RegEx.Replace(vSearch, "<b>$1</b>")
Set RegEx = Nothing
End Function
</SCRIPT>


 
Reply With Quote
 
David Morgan
Guest
Posts: n/a
 
      08-27-2004
I don't mean to be rude Steve but you really have missed the point on this
one.

Your last sentence is just wrong irrespective of my issue.

If I say Replace("something", "something else", vbTextCompare) then
"Something" would be converted to "something else". What I am looking for,
(and if you read the other posts in this thread), that has now been
provided, is to replace "Something" with "Something else" and "something"
with "something else". In your world I would need two replace statements.

I have only cited two variables here, "Something" and "something". There is
a large amount of variables regarding the case of the word being searched.
I want the replacement to be in the same case as the occurrence it replaced,
not in the case of the value being searched for.

Hope this clears it up.


"Steven Burn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Not really....... you wanted to know why "Something" was treated as
> "something"..??
>
> Although I know you can, I can't see any point in using RegExp for

something
> that can be done with the native Replace() function in one line of code.
>
> What your RegExp is doing with "RegExp.IgnoreCase = True" is essentially

the
> same as converting the query to lowercase before replacing the word/phrase
>
> --
>
> Regards
>
> Steven Burn
> Ur I.T. Mate Group
> www.it-mate.co.uk
>
> Keeping it FREE!
>
>
> "David Morgan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Err... thanks, but you have missed my point.
> >
> > "Steven Burn" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Function Highlight(sFind, sSearch)
> > > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

> > sFind
> > > & "</span>")
> > > End Function
> > >
> > > --
> > >
> > > Regards
> > >
> > > Steven Burn
> > > Ur I.T. Mate Group
> > > www.it-mate.co.uk
> > >
> > > Keeping it FREE!
> > >
> > >
> > > "David Morgan" <(E-Mail Removed)> wrote in message
> > > news:#(E-Mail Removed)...
> > > > Hello
> > > >
> > > > I have a little function to highlight text if it exists.
> > > >
> > > > Function Highlight(vFind, vSearch)
> > > > Dim RegEx
> > > > Set RegEx = New RegExp
> > > > RegEx.Pattern = vFind
> > > > RegEx.IgnoreCase = True
> > > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" &

> vFind
> > &
> > > > "</span>")
> > > > Set RegEx = Nothing
> > > > End Function
> > > >
> > > > The only problem is, that if I search for "something", "something"

> > appears
> > > > highlighted when the string being searched actually contained

> > "Something".
> > > > This is not about case sensitive searching, this is about showing

the
> > > actual
> > > > match rather than what was being searched for. Hope that makes

sense.
> > > >
> > > > For example:
> > > >
> > > > Response.Write Highlight("something", "Have you seen Something

About
> > > > Mary?")
> > > >
> > > > Gives:
> > > >
> > > > Have you seen <span class="Highlight">something</span> About Mary?
> > > >
> > > > Whilst this is ok, it would be great if it actually highlighted the

> > match
> > > > rather than the 'find, i.e:
> > > >
> > > > Have you seen <span class="Highlight">Something</span> About Mary?
> > > >
> > > > I have looked in to the documentation a bit and it seems that to do

> what
> > I
> > > > want involves using the execute method and then hunting through the
> > > matches
> > > > collection. Does anyone have any sample code for this that I could
> > > > _borrow_?
> > > >
> > > > My function is nice and small and it would be a shame to have to go

> for
> > > > something bigger, (a.k.a. performance degradation), if a simple

change
> > can
> > > > be made to my function.
> > > >
> > > > I am open to suggestions on the JScript approach also as I notice

> these
> > > > special characters $0 - $9 although MSDN is somewhat lacking in

sample
> > > code
> > > > for a novice like me.
> > > >
> > > > MTIA
> > > >
> > > > David
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Looking for app to search for & highlight text in any app chuckcar Computer Support 0 04-27-2010 10:45 PM
Search in Dataview and Highlight Text Robert Bevington ASP .Net Web Controls 0 06-06-2008 06:30 PM
Highlight on in search (FF 2) RalphOcean Firefox 0 12-21-2006 06:14 PM
Dispaly Results, then highlight closest matched row ASP.NET/ADO.NE Daniel Di Vita ASP General 2 04-21-2006 06:23 PM
RE:a problem , highlight word in the JTextPane? pvii007 Java 2 05-21-2005 02:52 PM



Advertisments