Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 29th, 2004, 09:32 PM
Authorized User
 
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default preserve caps with regexp.replace

hi, i have a function that highlights search keywords

function highlight(text, searchtxt)
if text <> empty and searchtxt <> empty then
  dim regex
  set regex = new regexp

  regex.pattern = searchtxt
  regex.global = true
  regex.ignorecase = true

  highlight = regex.replace(text, "" &
  searchtxt & "")
end if
end function

as you can see, this will allow the search to be case-insensitive, however, this also means that all keywords found in the text will be replace with the search word, e.g. HELLO replaced with hello(search word)
is there anyway to preserve the original text??? HELLO will be highlighted even though the user search for 'hello', and the second heLLo will also be highlighted as it is, anyone any ideas??

 
Old March 1st, 2004, 02:54 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Untested, you need parentheses around the expression. $1, $2 etc. is the result of the match:
Code:
function highlight(text, searchtxt)        
if text <> empty and searchtxt <> empty then
  dim regex
  set regex = new regexp
  
  regex.pattern = "(" & searchtxt & ")"
  regex.global = true
  regex.ignorecase = true
  
  highlight = regex.replace(text, "$1")
end if
end function
--

Joe
 
Old March 1st, 2004, 10:08 PM
Authorized User
 
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thnks joefawcett

it works bt i dun understand why, how does adding the parentheses helps to preserve the original text, and why would "$1" work, care to explain??

 
Old March 1st, 2004, 11:10 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Your original replace statement was this:

highlight = regex.replace(text, "" & searchtxt & "")

In it you are providing the search term (in whatever case was passed into the function call) as the replacement (within your additional HTML) for the found term.

Joe's suggestion:
highlight = regex.replace(text, "$1")

Here, the "$1" is a replacement token for the pattern's first search result found. So, the regex replace method is actually substituting "$1" with the match pattern found. This is a standard feature of regular expression replaces.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old March 2nd, 2004, 12:25 AM
Authorized User
 
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i see, thnks






Similar Threads
Thread Thread Starter Forum Replies Last Post
preserve spaces in boundfield Ivision ASP.NET 2.0 Basics 0 April 29th, 2008 03:16 AM
Preserve Formatting on Update Squarecat Word VBA 1 September 29th, 2006 08:33 AM
Re: need to convert data entered to all caps flyfish Access 5 March 18th, 2005 12:35 PM
Using Caps in Code gnotq Javascript 3 October 21st, 2004 11:36 AM
problem to "on caps lock progametically Abhinav_jain_mca General .NET 3 August 5th, 2004 03:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.