|
Subject:
|
highlight search words
|
|
Posted By:
|
lucian
|
Post Date:
|
1/20/2006 12:18:50 PM
|
hi guys,
i am using at the moment this type of regular expression to highlight a word from the search form.
function ShowBold(theField) Set objRegExp= New RegExp objRegExp.Pattern="(" & searchparam & ")" objRegExp.IgnoreCase=True objRegExp.Global=True ShowBold=objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>") end function
i would like to be able to highlight every ocurence of the world if there are more then one and the words are not beside each other.
Ex. text: "i have a cd player that will play cd and also mp3"
if i search for cd player i get the "cd player" highlight it but not the cd on its own (the one in bold)
|
|
Reply By:
|
Imar
|
Reply Date:
|
1/20/2006 1:51:38 PM
|
Hi lucian,
Probably the easiest way to do this is to break apart your search term into an array based on a space. Then for each item in the array, call the highlighting code. That way, each individual word is highlighted.
HtH,
Imar --------------------------------------- Imar Spaanjaars Everyone is unique, except for me.
|
|
Reply By:
|
lucian
|
Reply Date:
|
1/20/2006 3:03:15 PM
|
i am already doing this but i don't know where to use the expression:
Function BasicSearchSQL(Keyword) Dim sKeyword sKeyword = AdjustSql(Keyword) BasicSearchSQL = "" BasicSearchSQL = BasicSearchSQL & "[ProductName] LIKE '%" & sKeyword & "%' OR " BasicSearchSQL = BasicSearchSQL & "[Description] LIKE '%" & sKeyword & "%' OR " If Right(BasicSearchSQL, 4) = " OR " or Right(BasicSearchSQL, 4) = " or " Then BasicSearchSQL = Left(BasicSearchSQL, Len(BasicSearchSQL)-4) End Function
' Function SetUpBasicSearch ' - Set up Basic Search parameter based on form elements pSearch & pSearchType ' - Variables setup: sSrchBasic
Sub SetUpBasicSearch() Dim sSearch, sSearchType, arKeyword, sKeyword sSearch = searchparam sSearchType = searchtype If sSearch <> "" Then If sSearchType <> "" Then While InStr(sSearch, " ") > 0 sSearch = Replace(sSearch, " ", " ") Wend arKeyword = Split(Trim(sSearch), " ") For Each sKeyword In arKeyword sSrchBasic = sSrchBasic & " " & BasicSearchSQL(sKeyword) & " " & sSearchType & " " Next Else sSrchBasic = BasicSearchSQL(sSearch) End If End If If Right(sSrchBasic, 4) = " OR " or Right(sSrchBasic, 4) = " or " Then sSrchBasic = Left(sSrchBasic, Len(sSrchBasic)-4) If Right(sSrchBasic, 5) = " AND " or Right(sSrchBasic, 5) = " and " Then sSrchBasic = Left(sSrchBasic, Len(sSrchBasic)-5) End Sub
then i use this to display the results coming from the database:
<%= ShowBold(x_ProductName) %>
|
|
Reply By:
|
Imar
|
Reply Date:
|
1/20/2006 3:45:18 PM
|
You can pass the search term to ShowBold as well, and repeat the loop. The following (untested) code shows you what I mean:
Function ShowBold(theField, searchTerm)
Dim item Set objRegExp = New RegExp
For Each item in Split(searchTerm, " ") objRegExp.Pattern="(" & item & ")" objRegExp.IgnoreCase = True objRegExp.Global = True ShowBold = objRegExp.Replace(theField,"<b style=""background- color:#FFFF00; font-weight:bold;"">$1</b>") Next End Function
Then call it like this:
<%= ShowBold(x_ProductName, sSearch) %>
It may not work directly, but I am sure you get the idea...
Imar --------------------------------------- Imar Spaanjaars Everyone is unique, except for me.
|
|
Reply By:
|
lucian
|
Reply Date:
|
1/20/2006 6:00:48 PM
|
i have this:
function ShowBold(theField) sSearchs = searchparam While InStr(sSearchs, " ") > 0 sSearchs = Replace(sSearchs, " ", " ") Wend arKeywords = Split(Trim(sSearchs), " ") Set objRegExp= New RegExp For Each sKeywords In arKeywords objRegExp.Pattern="(" & sKeywords & ")" objRegExp.IgnoreCase=True objRegExp.Global=True ShowBold=objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>") next end function
but it only highlights the last word if i have more then one
|
|
Reply By:
|
Imar
|
Reply Date:
|
1/20/2006 6:27:22 PM
|
My fault. This code keeps using the input text inside the loop, but it should reuse the result of each hightlighting:
Dim tempResult tempResult = theField sSearchs = searchparam While InStr(sSearchs, " ") > 0 sSearchs = Replace(sSearchs, " ", " ") Wend arKeywords = Split(Trim(sSearchs), " ") Set objRegExp= New RegExp For Each sKeywords In arKeywords objRegExp.Pattern="(" & sKeywords & ")" objRegExp.IgnoreCase=True objRegExp.Global=True theField = objRegExp.Replace(theField,"<b style=""background-color:#FFFF00; font-weight:bold;"">$1</b>") next
ShowBold = theField
Personally, I think it's cleaner to have ShowBold accept the search term as a parameter. That way, it's easier to reuse the method because it doesn't rely on global variables.
Cheers,
Imar --------------------------------------- Imar Spaanjaars Everyone is unique, except for me.
|
|
Reply By:
|
lucian
|
Reply Date:
|
1/21/2006 3:30:20 AM
|
thanks Imar. As usual you are right. I was working on it all day yesterday, and the solution is so simple.
|