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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting Query Side and Code Side

Search Utilities

Highlighting Query Side and Code Side

by  webmigit  Posted    (Edited  )
Glowball wrote an excellent piece of code for highlighting (faq232-1167).. but I felt it lacked in a few areas.. Here is where I add what I can to the cause

Code:
<CFSET LocalTD=ReReplaceNoCase(LocalTD,"[^A-Za-z0-9](#currword#)[^A-Za-z0-9]"," <span class=""highlight"">\1</SPAN> ","ALL")>

The above code is used to highlight the word when it appears as itself.. it can be followed by any punctuation but has to be a word of its own..

The [^A-Za-z0-9] before (#currword#) tells cold fusion to search for the string when not preceded by any alphanumeric character.. The [^A-Za-z0-9] after (#currword#) likewise tells Cold Fusion to search for the word in cases where it is not followed by an alphanumeric character..

Placing #currword# in parentheses creates the temporary variable \1.. We use this variable to replace our undefined characters with those same undefined characters surrounded by something..

In my case, I surrounded the string which my code did not know at design time with <span></span> to create a highlight-marker effect. Should you want the same effect, add this to your style sheet:

Code:
.highlight {  background-color: #FFFF00}

Then there is the issue I came across.. I wanted to find my keywords but I didn't want to return results from my query that contained my keywords but not as a whole.. my query was returning things like this:

Code:
+------------------------------------------+
| WORD | Matches                           |
+------------------------------------------+
|  AN  | and, than, candle, etc            |
+------------------------------------------+
|  HE  | the, they, hell, she, then, there |
+------------------------------------------+

This obviously caused problems.. And no one seemed to know what to do about it.. I settled for a very complex regex that ran a like statement for every concievable condition my word could be in.

I will save you the trouble, the headaches, the long coding hours, and the psychiatric bills caused by doing it that way.

Access has an extensive wildcard capability.. much like the find syntax for regex.. except that MS can never seem to comply to a standard completely (which admittedly, is why IE dominates the browser field)... I assumed this and tried to run my like statement like this:

Code:
dbField LIKE '%[^A-Za-z0-9]#myKeyword#[^A-Za-z0-9]%'

It was a failure, the sql did not function properly so I went back to my complex regex that ran about 15 LIKEs per term.. a royal headahce.. And then I was looking through the MS Access help files and found that MS, for Access at least, had their own way of being different for this issue.. Instead of using the carat (^) in brackets to signify characters that cannot be there, MS uses the exclamation point (!).. My statement now became:

Code:
dbField LIKE '%[!A-Za-z0-9]#myKeyword#[!A-Za-z0-9]%'

And it works perfectly..

So if you want to do advanced searching without some third party tag and want direct control of your queries, this code is your start.

If this helps, please rate it!

Tony Hicks
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