Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting Search Terms

Search Utilities

Highlighting Search Terms

by  Glowball  Posted    (Edited  )
If you search for "time" using a search utility, how do you highlight the word "time" throughout the results, without also highlighting "sometime" or "timeframe" or other variations? The trick is to use Regular Expressions, or pattern matching.

This assumes someone submitted a form with a keyword to be searched. These tags work on the results before they are displayed.
Code:
<cfset keyword = FORM.keyword>
<cfset oldResults = VARIABLES.results>
<cfset newResults = REReplaceNoCase(VARIABLES.oldResults, "(([^A-Za-z])(#Trim(VARIABLES.keyword)#)([^A-Za-z]))", "\2<b  style='background=color:yellow'>\3</b>\4", "ALL")>
<cfif Left(VARIABLES.newResults, Len(Trim(VARIABLES.keyword))) IS Trim(VARIABLES.keyword)>
    <cfset newResults = REReplaceNoCase(VARIABLES.newResults, "((#Trim(VARIABLES.keyword)#)([^A-Za-z]))", "<b style='background-color:yellow'>\2</b>\3", "ONE")>
</cfif>
What does the [tt]\2 \3[/tt] and [tt]\4[/tt] mean? These are numbers to references pieces of the input to a regular expression function like [tt]REReplaceNoCase()[/tt]. For example, see the following:

[tt](\1 (\2) (\3) (\4 (\5) ) )[/tt]

Look at the placement of parentheses and numbers, \1 starts at the outermost pair of parentheses, and working your way in increases every number. If they are on the same level of the hierarchy of parentheses, it goes from left to right in the expression.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top