Highlight Search Word in Results

D

David Morgan

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
 
S

Steven Burn

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!
 
C

Chris Hohmann

David Morgan said:
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
 
D

David Morgan

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
 
D

David Morgan

Err... thanks, but you have missed my point.

Steven Burn said:
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!
 
D

David Morgan

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
 
A

Aaron [SQL Server MVP]

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...
 
S

Steven Burn

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!
 
D

David Morgan

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>
 
D

David Morgan

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.
 
D

David Morgan

A-ha!

Spotted it. I was missing parenthesis. My function should look like this.

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

This still does not explain why $1 works, save me being on old docs :S

Thanks for all your input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top