|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Replace Question
I have a search results pages and I'd like to highlight the search term to show the matches. Of course, search is not case sensitive.Robert Harrison 06/27/12 02:25 P I have a search results pages and I'd like to highlight the search term to show the matches. Of course, search is not case sensitive.Robert Harrison 06/27/12 02:30 P Is there something in your CSS that is changing it to lower case?Eric Roberts 06/27/12 06:57 P This is a case for Regular Expressions (RegEx):Michael Dinowitz 06/27/12 02:42 P > # REReplaceNoCase(answer, '(#search_string#)', '<span class="keyword">\1</span>', 'all')#Robert Harrison 06/27/12 02:51 P Stop nesting #s!! You're needlessly killing kittens!!!Matt Quackenbush 06/27/12 02:57 P > Stop nesting #s. You're needlessly killing kittens!!!Robert Harrison 06/27/12 03:09 P I hope you noticed that I posted that _because_ you said you were waitingMatt Quackenbush 06/27/12 03:37 P Got it.Robert Harrison 06/27/12 04:02 P On Wed, Jun 27, 2012 at 2:51 PM, Robert Harrison <robert@austin-williams.comMichael Dinowitz 06/27/12 03:02 P >This is a case for Regular Expressions (RegEx):Peter Boughton 07/02/12 09:23 A I have a search results pages and I'd like to highlight the search term to show the matches. Of course, search is not case sensitive. On the results I can do something like: ReplaceNoCase(answer, search_string,"<span class=""keyword"">#search_string#</span>","all")# This puts a nice little highlight around the search term and makes it easy to see in the results, but it may change the case. For example, if I search for "mick jagger" and it finds "Mick Jagger", my answer come out like: ... and a "mick jagger" was guy with big lips who dreamed of being a Beatle... Is there a way I could do the replace to highlight the result and NOT change the case? Thanks, Robert Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_ I have a search results pages and I'd like to highlight the search term to show the matches. Of course, search is not case sensitive. On the results I can do something like: Better written results: #ReplaceNoCase(answer, search_string,"<span class=""keyword"">" & search_string & "</span>","all")# This puts a nice little highlight around the search term and makes it easy to see in the results, but it may change the case. For example, if I search for "mick jagger" and it finds "Mick Jagger", my answer come out like: ... and a "mick jagger" was guy with big lips who dreamed of being a Beatle... Is there a way I could do the replace to highlight the result and NOT change the case? Thanks, Robert Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_ Is there something in your CSS that is changing it to lower case? Eric Roberts Owner/Developer Three Ravens Consulting owner@threeravensconsulting.com http://www.threeravensconsulting.com tel: 630-881-1515 I have a search results pages and I'd like to highlight the search term to show the matches. Of course, search is not case sensitive. On the results I can do something like: Better written results: #ReplaceNoCase(answer, search_string,"<span class=""keyword"">" & search_string & "</span>","all")# This puts a nice little highlight around the search term and makes it easy to see in the results, but it may change the case. For example, if I search for "mick jagger" and it finds "Mick Jagger", my answer come out like: ... and a "mick jagger" was guy with big lips who dreamed of being a Beatle... Is there a way I could do the replace to highlight the result and NOT change the case? Thanks, Robert Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_ This is a case for Regular Expressions (RegEx): REReplaceNoCase(answer, '(#search_string#)', '<span class="keyword">\1</span>', 'all')# 1. Search the content of the variable answer 2. Look for the content of the variable search_string 2a. Because parenthesis are being used and we're doing RegEx, the matched text will be 'captured' for later use. The parenthesis are special characters, not part of what is being matched. 3. Replace the matched content with: <span class="keyword">\1</span> 3a. take the captured text from the match and place it within the span block in place of the \1 \1 will contain the exact text matched with whatever case it had rather than the case of the search_string On Wed, Jun 27, 2012 at 2:25 PM, Robert Harrison <robert@austin-williams.com ----- Excess quoted text cut - see Original Post for more ----- > # REReplaceNoCase(answer, '(#search_string#)', '<span class="keyword">\1</span>', 'all')# Nice. Works perfectly. Thank You... just waiting for someone to complain about the nested ##'s though LOL :-) Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_ Stop nesting #s!! You're needlessly killing kittens!!! :D #reReplaceNoCase( answer, search_string, '<span class="keyword">\1</span>', 'all' )# ;-) On Wed, Jun 27, 2012 at 1:51 PM, Robert Harrison <robert@austin-williams.com ----- Excess quoted text cut - see Original Post for more ----- > Stop nesting #s. You're needlessly killing kittens!!! I was sooo waiting for that shoe to drop :-) Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_ I hope you noticed that I posted that _because_ you said you were waiting on it. ;-) On Wed, Jun 27, 2012 at 2:08 PM, Robert Harrison <robert@austin-williams.com ----- Excess quoted text cut - see Original Post for more ----- Got it. Robert Harrison Director of Interactive Services Austin & Williams Advertising I Branding I Digital I Direct 125 Kennedy Drive, Suite 100 I Hauppauge, NY 11788 T 631.231.6600 X 119 F 631.434.7022 http://www.austin-williams.com Blog: http://www.austin-williams.com/blog Twitter: http://www.twitter.com/austin_williams I hope you noticed that I posted that _because_ you said you were waiting on it. ;-) On Wed, Jun 27, 2012 at 2:08 PM, Robert Harrison <robert@austin-williams.com ----- Excess quoted text cut - see Original Post for more ----- On Wed, Jun 27, 2012 at 2:51 PM, Robert Harrison <robert@austin-williams.com ----- Excess quoted text cut - see Original Post for more ----- '(' & search_string & ')' I drop all attempts at being anal about #s when I'm working with RegEx. RegEx is anal enough. >This is a case for Regular Expressions (RegEx): > >REReplaceNoCase(answer, '(#search_string#)', '<span >class="keyword">\1</span>', 'all')# > Heh, just seen this after the other thread, so guess I'll repeat what I said there: Using parentheses is completely unnecessary. Use \0 in the replacement string instead. If search_string is (for example) "$10" or ":)" or "a+b^c" or whatever then simply using it as a regex_pattern will cause problems. To solve this means escaping the meta-characters, for example, search_string.replaceAll('[$^*()+\[\]{}.?\\|]','\\$0') Putting both those together results in: REReplaceNoCase ( answer , search_string.replaceAll('[$^*()+\[\]{}.?\\|]','\\$0') , '<span class="keyword">\0</span>' , 'all' ) (Though of course, as noted in the other thread, this doesn't deal with HTML.)
|
May 23, 2013
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||