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

February 29th, 2004, 09:32 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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??
|
|

March 1st, 2004, 02:54 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|

March 1st, 2004, 10:08 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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??
|
|

March 1st, 2004, 11:10 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

March 2nd, 2004, 12:25 AM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i see, thnks
|
|
 |