Highlight Search Word in Results

Discussion in 'ASP General' started by David Morgan, Aug 27, 2004.

  1. David Morgan

    David Morgan Guest

    Hello

    I have a little function to highlight text if it exists.

    Function Highlight(vFind, vSearch)
    Dim RegEx
    Set RegEx = New RegExp
    RegEx.Pattern = vFind
    RegEx.IgnoreCase = True
    Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
    "</span>")
    Set RegEx = Nothing
    End Function

    The only problem is, that if I search for "something", "something" appears
    highlighted when the string being searched actually contained "Something".
    This is not about case sensitive searching, this is about showing the actual
    match rather than what was being searched for. Hope that makes sense.

    For example:

    Response.Write Highlight("something", "Have you seen Something About
    Mary?")

    Gives:

    Have you seen <span class="Highlight">something</span> About Mary?

    Whilst this is ok, it would be great if it actually highlighted the match
    rather than the 'find, i.e:

    Have you seen <span class="Highlight">Something</span> About Mary?

    I have looked in to the documentation a bit and it seems that to do what I
    want involves using the execute method and then hunting through the matches
    collection. Does anyone have any sample code for this that I could
    _borrow_?

    My function is nice and small and it would be a shame to have to go for
    something bigger, (a.k.a. performance degradation), if a simple change can
    be made to my function.

    I am open to suggestions on the JScript approach also as I notice these
    special characters $0 - $9 although MSDN is somewhat lacking in sample code
    for a novice like me.

    MTIA

    David
     
    David Morgan, Aug 27, 2004
    #1
    1. Advertising

  2. David Morgan

    Steven Burn Guest

    Function Highlight(sFind, sSearch)
    Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" & sFind
    & "</span>")
    End Function

    --

    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!


    "David Morgan" <> wrote in message
    news:#...
    > Hello
    >
    > I have a little function to highlight text if it exists.
    >
    > Function Highlight(vFind, vSearch)
    > Dim RegEx
    > Set RegEx = New RegExp
    > RegEx.Pattern = vFind
    > RegEx.IgnoreCase = True
    > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
    > "</span>")
    > Set RegEx = Nothing
    > End Function
    >
    > The only problem is, that if I search for "something", "something" appears
    > highlighted when the string being searched actually contained "Something".
    > This is not about case sensitive searching, this is about showing the

    actual
    > match rather than what was being searched for. Hope that makes sense.
    >
    > For example:
    >
    > Response.Write Highlight("something", "Have you seen Something About
    > Mary?")
    >
    > Gives:
    >
    > Have you seen <span class="Highlight">something</span> About Mary?
    >
    > Whilst this is ok, it would be great if it actually highlighted the match
    > rather than the 'find, i.e:
    >
    > Have you seen <span class="Highlight">Something</span> About Mary?
    >
    > I have looked in to the documentation a bit and it seems that to do what I
    > want involves using the execute method and then hunting through the

    matches
    > collection. Does anyone have any sample code for this that I could
    > _borrow_?
    >
    > My function is nice and small and it would be a shame to have to go for
    > something bigger, (a.k.a. performance degradation), if a simple change can
    > be made to my function.
    >
    > I am open to suggestions on the JScript approach also as I notice these
    > special characters $0 - $9 although MSDN is somewhat lacking in sample

    code
    > for a novice like me.
    >
    > MTIA
    >
    > David
    >
    >
     
    Steven Burn, Aug 27, 2004
    #2
    1. Advertising

  3. "David Morgan" <> wrote in message
    news:%...
    > Hello
    >
    > I have a little function to highlight text if it exists.
    >
    > Function Highlight(vFind, vSearch)
    > Dim RegEx
    > Set RegEx = New RegExp
    > RegEx.Pattern = vFind
    > RegEx.IgnoreCase = True
    > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
    > "</span>")
    > Set RegEx = Nothing
    > End Function
    >
    > The only problem is, that if I search for "something", "something" appears
    > highlighted when the string being searched actually contained "Something".
    > This is not about case sensitive searching, this is about showing the

    actual
    > match rather than what was being searched for. Hope that makes sense.
    >
    > For example:
    >
    > Response.Write Highlight("something", "Have you seen Something About
    > Mary?")
    >
    > Gives:
    >
    > Have you seen <span class="Highlight">something</span> About Mary?
    >
    > Whilst this is ok, it would be great if it actually highlighted the match
    > rather than the 'find, i.e:
    >
    > Have you seen <span class="Highlight">Something</span> About Mary?
    >
    > I have looked in to the documentation a bit and it seems that to do what I
    > want involves using the execute method and then hunting through the

    matches
    > collection. Does anyone have any sample code for this that I could
    > _borrow_?
    >
    > My function is nice and small and it would be a shame to have to go for
    > something bigger, (a.k.a. performance degradation), if a simple change can
    > be made to my function.
    >
    > I am open to suggestions on the JScript approach also as I notice these
    > special characters $0 - $9 although MSDN is somewhat lacking in sample

    code
    > for a novice like me.


    http://aspfaq.com/show.asp?id=2344
     
    Chris Hohmann, Aug 27, 2004
    #3
  4. David Morgan

    David Morgan Guest

    Hi Curt

    Thanks for your suggestion. I may end up falling back to it, but I was
    really trying to avoid string concatenation and looping as per my
    penultimate sentence.

    Regards

    David


    "Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
    news:ePdw$K%...
    > http://www.darkfalz.com/1089
    >
    > --
    > Curt Christianson
    > Owner/Lead Developer, DF-Software
    > Site: http://www.Darkfalz.com
    > Blog: http://blog.Darkfalz.com
    >
    >
    > "David Morgan" <> wrote in message
    > news:%...
    > > Hello
    > >
    > > I have a little function to highlight text if it exists.
    > >
    > > Function Highlight(vFind, vSearch)
    > > Dim RegEx
    > > Set RegEx = New RegExp
    > > RegEx.Pattern = vFind
    > > RegEx.IgnoreCase = True
    > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

    &
    > > "</span>")
    > > Set RegEx = Nothing
    > > End Function
    > >
    > > The only problem is, that if I search for "something", "something"

    appears
    > > highlighted when the string being searched actually contained

    "Something".
    > > This is not about case sensitive searching, this is about showing the
    > > actual
    > > match rather than what was being searched for. Hope that makes sense.
    > >
    > > For example:
    > >
    > > Response.Write Highlight("something", "Have you seen Something About
    > > Mary?")
    > >
    > > Gives:
    > >
    > > Have you seen <span class="Highlight">something</span> About Mary?
    > >
    > > Whilst this is ok, it would be great if it actually highlighted the

    match
    > > rather than the 'find, i.e:
    > >
    > > Have you seen <span class="Highlight">Something</span> About Mary?
    > >
    > > I have looked in to the documentation a bit and it seems that to do what

    I
    > > want involves using the execute method and then hunting through the
    > > matches
    > > collection. Does anyone have any sample code for this that I could
    > > _borrow_?
    > >
    > > My function is nice and small and it would be a shame to have to go for
    > > something bigger, (a.k.a. performance degradation), if a simple change

    can
    > > be made to my function.
    > >
    > > I am open to suggestions on the JScript approach also as I notice these
    > > special characters $0 - $9 although MSDN is somewhat lacking in sample
    > > code
    > > for a novice like me.
    > >
    > > MTIA
    > >
    > > David
    > >
    > >

    >
    >
     
    David Morgan, Aug 27, 2004
    #4
  5. David Morgan

    David Morgan Guest

    Err... thanks, but you have missed my point.

    "Steven Burn" <> wrote in message
    news:...
    > Function Highlight(sFind, sSearch)
    > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

    sFind
    > & "</span>")
    > End Function
    >
    > --
    >
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    >
    > "David Morgan" <> wrote in message
    > news:#...
    > > Hello
    > >
    > > I have a little function to highlight text if it exists.
    > >
    > > Function Highlight(vFind, vSearch)
    > > Dim RegEx
    > > Set RegEx = New RegExp
    > > RegEx.Pattern = vFind
    > > RegEx.IgnoreCase = True
    > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

    &
    > > "</span>")
    > > Set RegEx = Nothing
    > > End Function
    > >
    > > The only problem is, that if I search for "something", "something"

    appears
    > > highlighted when the string being searched actually contained

    "Something".
    > > This is not about case sensitive searching, this is about showing the

    > actual
    > > match rather than what was being searched for. Hope that makes sense.
    > >
    > > For example:
    > >
    > > Response.Write Highlight("something", "Have you seen Something About
    > > Mary?")
    > >
    > > Gives:
    > >
    > > Have you seen <span class="Highlight">something</span> About Mary?
    > >
    > > Whilst this is ok, it would be great if it actually highlighted the

    match
    > > rather than the 'find, i.e:
    > >
    > > Have you seen <span class="Highlight">Something</span> About Mary?
    > >
    > > I have looked in to the documentation a bit and it seems that to do what

    I
    > > want involves using the execute method and then hunting through the

    > matches
    > > collection. Does anyone have any sample code for this that I could
    > > _borrow_?
    > >
    > > My function is nice and small and it would be a shame to have to go for
    > > something bigger, (a.k.a. performance degradation), if a simple change

    can
    > > be made to my function.
    > >
    > > I am open to suggestions on the JScript approach also as I notice these
    > > special characters $0 - $9 although MSDN is somewhat lacking in sample

    > code
    > > for a novice like me.
    > >
    > > MTIA
    > >
    > > David
    > >
    > >

    >
    >
     
    David Morgan, Aug 27, 2004
    #5
  6. David Morgan

    David Morgan Guest

    Thanks Chris

    When I adapt your VBScript example from ASP FAQ, which is almost identical
    to mine, I get $1 instead of the match in the correct case.

    If you read the documentation regarding regular expressions you will see:

    (pattern) Matches pattern and captures the match. The captured match
    can be retrieved from the resulting Matches collection, using the SubMatches
    collection in VBScript or the $0.$9 properties in JScript. To match
    parentheses characters ( ), use '\(' or '\)'.



    In other words, the $0 - $9 properties are not available in VBScript. Have
    I missed something here or has Aaron included something on the FAQ without
    testing it!

    With a little bit of modification I have managed to convert your Javascript
    example in to this, which does the job, (and matches whole words only,
    unlike my VBScript version).

    function jsHighlight(vFind, vSearch) {
    var re = new RegExp('\\b(' + vFind + ')\\b', 'gi')
    return vSearch.replace(re, '\$1')
    }

    Thanks and regards

    David


    "Chris Hohmann" <> wrote in message
    news:eOh69$%...
    > "David Morgan" <> wrote in message
    > news:%...
    > > Hello
    > >
    > > I have a little function to highlight text if it exists.
    > >
    > > Function Highlight(vFind, vSearch)
    > > Dim RegEx
    > > Set RegEx = New RegExp
    > > RegEx.Pattern = vFind
    > > RegEx.IgnoreCase = True
    > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind

    &
    > > "</span>")
    > > Set RegEx = Nothing
    > > End Function
    > >
    > > The only problem is, that if I search for "something", "something"

    appears
    > > highlighted when the string being searched actually contained

    "Something".
    > > This is not about case sensitive searching, this is about showing the

    > actual
    > > match rather than what was being searched for. Hope that makes sense.
    > >
    > > For example:
    > >
    > > Response.Write Highlight("something", "Have you seen Something About
    > > Mary?")
    > >
    > > Gives:
    > >
    > > Have you seen <span class="Highlight">something</span> About Mary?
    > >
    > > Whilst this is ok, it would be great if it actually highlighted the

    match
    > > rather than the 'find, i.e:
    > >
    > > Have you seen <span class="Highlight">Something</span> About Mary?
    > >
    > > I have looked in to the documentation a bit and it seems that to do what

    I
    > > want involves using the execute method and then hunting through the

    > matches
    > > collection. Does anyone have any sample code for this that I could
    > > _borrow_?
    > >
    > > My function is nice and small and it would be a shame to have to go for
    > > something bigger, (a.k.a. performance degradation), if a simple change

    can
    > > be made to my function.
    > >
    > > I am open to suggestions on the JScript approach also as I notice these
    > > special characters $0 - $9 although MSDN is somewhat lacking in sample

    > code
    > > for a novice like me.

    >
    > http://aspfaq.com/show.asp?id=2344
    >
    >
     
    David Morgan, Aug 27, 2004
    #6
  7. > In other words, the $0 - $9 properties are not available in VBScript.
    Have
    > I missed something here or has Aaron included something on the FAQ without
    > testing it!


    ??? What are you talking about?

    The 'untested' sample you mention works perfectly fine here, both in JScript
    and in VBScript. Maybe the problem is in your "adaptation" of the code...

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)
     
    Aaron [SQL Server MVP], Aug 27, 2004
    #7
  8. David Morgan

    Steven Burn Guest

    Not really....... you wanted to know why "Something" was treated as
    "something"..??

    Although I know you can, I can't see any point in using RegExp for something
    that can be done with the native Replace() function in one line of code.

    What your RegExp is doing with "RegExp.IgnoreCase = True" is essentially the
    same as converting the query to lowercase before replacing the word/phrase

    --

    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!


    "David Morgan" <> wrote in message
    news:...
    > Err... thanks, but you have missed my point.
    >
    > "Steven Burn" <> wrote in message
    > news:...
    > > Function Highlight(sFind, sSearch)
    > > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

    > sFind
    > > & "</span>")
    > > End Function
    > >
    > > --
    > >
    > > Regards
    > >
    > > Steven Burn
    > > Ur I.T. Mate Group
    > > www.it-mate.co.uk
    > >
    > > Keeping it FREE!
    > >
    > >
    > > "David Morgan" <> wrote in message
    > > news:#...
    > > > Hello
    > > >
    > > > I have a little function to highlight text if it exists.
    > > >
    > > > Function Highlight(vFind, vSearch)
    > > > Dim RegEx
    > > > Set RegEx = New RegExp
    > > > RegEx.Pattern = vFind
    > > > RegEx.IgnoreCase = True
    > > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" &

    vFind
    > &
    > > > "</span>")
    > > > Set RegEx = Nothing
    > > > End Function
    > > >
    > > > The only problem is, that if I search for "something", "something"

    > appears
    > > > highlighted when the string being searched actually contained

    > "Something".
    > > > This is not about case sensitive searching, this is about showing the

    > > actual
    > > > match rather than what was being searched for. Hope that makes sense.
    > > >
    > > > For example:
    > > >
    > > > Response.Write Highlight("something", "Have you seen Something About
    > > > Mary?")
    > > >
    > > > Gives:
    > > >
    > > > Have you seen <span class="Highlight">something</span> About Mary?
    > > >
    > > > Whilst this is ok, it would be great if it actually highlighted the

    > match
    > > > rather than the 'find, i.e:
    > > >
    > > > Have you seen <span class="Highlight">Something</span> About Mary?
    > > >
    > > > I have looked in to the documentation a bit and it seems that to do

    what
    > I
    > > > want involves using the execute method and then hunting through the

    > > matches
    > > > collection. Does anyone have any sample code for this that I could
    > > > _borrow_?
    > > >
    > > > My function is nice and small and it would be a shame to have to go

    for
    > > > something bigger, (a.k.a. performance degradation), if a simple change

    > can
    > > > be made to my function.
    > > >
    > > > I am open to suggestions on the JScript approach also as I notice

    these
    > > > special characters $0 - $9 although MSDN is somewhat lacking in sample

    > > code
    > > > for a novice like me.
    > > >
    > > > MTIA
    > > >
    > > > David
    > > >
    > > >

    > >
    > >

    >
    >
     
    Steven Burn, Aug 27, 2004
    #8
  9. David Morgan

    David Morgan Guest

    Hi Aaron

    I believed that I had caveated that sentence enough so as not to insult. It
    would seem not. You will note it starts with "Have I missed something
    here". Please accept my apologies for your inference.

    The sentence you have cited below is a question, not a statement, excuse my
    poor grammar and the absence of a question mark.

    I was not surprised by my VBScript code not working as MSDN says $0-$9 is
    for JScript. I am now wholly confused as to why Chris' example _is_working!
    Maybe I'm on an old version of the VBScript docs or something.

    Here is my VBScript function adapted based on Chris' example:

    Function Highlight(vFind, vSearch)
    Dim RegEx
    Set RegEx = New RegExp
    RegEx.Pattern = "\b" & vFind & "\b"
    RegEx.IgnoreCase = True
    RegEx.Global = True
    Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">$1</span>")
    Set RegEx = Nothing
    End Function

    When I use the above, I get $1 literally returned, not the match like in
    Chris' example, (included at the end), and I'm confused as to why.

    If anyone has the time they could paste the whole thing below into an new
    ASP and see what they think.

    Thanks

    David M

    <% Dim re, strInput, strOutput
    strInput = "" & _
    "This a line of text containing the words one, two and three." &
    vbCRLF & _
    "This line ends with the word two" & vbCRLF & _
    "One is the word that starts this line." & vbCRLF & _
    "This line contains the word cone."

    Set re = New RegExp
    re.Pattern="\b(" & "One" & ")\b"
    re.IgnoreCase=True
    re.Global=True
    strOutput=re.Replace(strInput,"<b>$1</b>")
    Response.Write("<pre>" & strOutput & "</pre>")
    Response.Write "<hr>"
    Response.Write Highlight("One", strInput)%>
    <SCRIPT LANGUAGE=vbscript RUNAT=Server>
    Function Highlight(vFind, vSearch)
    Dim RegEx
    Set RegEx = New RegExp
    RegEx.Pattern = "\b" & vFind & "\b"
    RegEx.IgnoreCase = True
    RegEx.Global = True
    Highlight = RegEx.Replace(vSearch, "<b>$1</b>")
    Set RegEx = Nothing
    End Function
    </SCRIPT>
     
    David Morgan, Aug 27, 2004
    #9
  10. David Morgan

    David Morgan Guest

    I don't mean to be rude Steve but you really have missed the point on this
    one.

    Your last sentence is just wrong irrespective of my issue.

    If I say Replace("something", "something else", vbTextCompare) then
    "Something" would be converted to "something else". What I am looking for,
    (and if you read the other posts in this thread), that has now been
    provided, is to replace "Something" with "Something else" and "something"
    with "something else". In your world I would need two replace statements.

    I have only cited two variables here, "Something" and "something". There is
    a large amount of variables regarding the case of the word being searched.
    I want the replacement to be in the same case as the occurrence it replaced,
    not in the case of the value being searched for.

    Hope this clears it up.


    "Steven Burn" <> wrote in message
    news:...
    > Not really....... you wanted to know why "Something" was treated as
    > "something"..??
    >
    > Although I know you can, I can't see any point in using RegExp for

    something
    > that can be done with the native Replace() function in one line of code.
    >
    > What your RegExp is doing with "RegExp.IgnoreCase = True" is essentially

    the
    > same as converting the query to lowercase before replacing the word/phrase
    >
    > --
    >
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    >
    > "David Morgan" <> wrote in message
    > news:...
    > > Err... thanks, but you have missed my point.
    > >
    > > "Steven Burn" <> wrote in message
    > > news:...
    > > > Function Highlight(sFind, sSearch)
    > > > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">" &

    > > sFind
    > > > & "</span>")
    > > > End Function
    > > >
    > > > --
    > > >
    > > > Regards
    > > >
    > > > Steven Burn
    > > > Ur I.T. Mate Group
    > > > www.it-mate.co.uk
    > > >
    > > > Keeping it FREE!
    > > >
    > > >
    > > > "David Morgan" <> wrote in message
    > > > news:#...
    > > > > Hello
    > > > >
    > > > > I have a little function to highlight text if it exists.
    > > > >
    > > > > Function Highlight(vFind, vSearch)
    > > > > Dim RegEx
    > > > > Set RegEx = New RegExp
    > > > > RegEx.Pattern = vFind
    > > > > RegEx.IgnoreCase = True
    > > > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" &

    > vFind
    > > &
    > > > > "</span>")
    > > > > Set RegEx = Nothing
    > > > > End Function
    > > > >
    > > > > The only problem is, that if I search for "something", "something"

    > > appears
    > > > > highlighted when the string being searched actually contained

    > > "Something".
    > > > > This is not about case sensitive searching, this is about showing

    the
    > > > actual
    > > > > match rather than what was being searched for. Hope that makes

    sense.
    > > > >
    > > > > For example:
    > > > >
    > > > > Response.Write Highlight("something", "Have you seen Something

    About
    > > > > Mary?")
    > > > >
    > > > > Gives:
    > > > >
    > > > > Have you seen <span class="Highlight">something</span> About Mary?
    > > > >
    > > > > Whilst this is ok, it would be great if it actually highlighted the

    > > match
    > > > > rather than the 'find, i.e:
    > > > >
    > > > > Have you seen <span class="Highlight">Something</span> About Mary?
    > > > >
    > > > > I have looked in to the documentation a bit and it seems that to do

    > what
    > > I
    > > > > want involves using the execute method and then hunting through the
    > > > matches
    > > > > collection. Does anyone have any sample code for this that I could
    > > > > _borrow_?
    > > > >
    > > > > My function is nice and small and it would be a shame to have to go

    > for
    > > > > something bigger, (a.k.a. performance degradation), if a simple

    change
    > > can
    > > > > be made to my function.
    > > > >
    > > > > I am open to suggestions on the JScript approach also as I notice

    > these
    > > > > special characters $0 - $9 although MSDN is somewhat lacking in

    sample
    > > > code
    > > > > for a novice like me.
    > > > >
    > > > > MTIA
    > > > >
    > > > > David
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    David Morgan, Aug 27, 2004
    #10
  11. David Morgan

    David Morgan Guest

    A-ha!

    Spotted it. I was missing parenthesis. My function should look like this.

    Function Highlight(vFind, vSearch)
    Dim RegEx
    Set RegEx = New RegExp
    RegEx.Pattern = "\b(" & vFind & ")\b"
    RegEx.IgnoreCase = True
    RegEx.Global = True
    Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">$1</span>")
    Set RegEx = Nothing
    End Function

    This still does not explain why $1 works, save me being on old docs :S

    Thanks for all your input.



    "David Morgan" <> wrote in message
    news:...
    > Hi Aaron
    >
    > I believed that I had caveated that sentence enough so as not to insult.

    It
    > would seem not. You will note it starts with "Have I missed something
    > here". Please accept my apologies for your inference.
    >
    > The sentence you have cited below is a question, not a statement, excuse

    my
    > poor grammar and the absence of a question mark.
    >
    > I was not surprised by my VBScript code not working as MSDN says $0-$9 is
    > for JScript. I am now wholly confused as to why Chris' example

    _is_working!
    > Maybe I'm on an old version of the VBScript docs or something.
    >
    > Here is my VBScript function adapted based on Chris' example:
    >
    > Function Highlight(vFind, vSearch)
    > Dim RegEx
    > Set RegEx = New RegExp
    > RegEx.Pattern = "\b" & vFind & "\b"
    > RegEx.IgnoreCase = True
    > RegEx.Global = True
    > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">$1</span>")
    > Set RegEx = Nothing
    > End Function
    >
    > When I use the above, I get $1 literally returned, not the match like in
    > Chris' example, (included at the end), and I'm confused as to why.
    >
    > If anyone has the time they could paste the whole thing below into an new
    > ASP and see what they think.
    >
    > Thanks
    >
    > David M
    >
    > <% Dim re, strInput, strOutput
    > strInput = "" & _
    > "This a line of text containing the words one, two and three." &
    > vbCRLF & _
    > "This line ends with the word two" & vbCRLF & _
    > "One is the word that starts this line." & vbCRLF & _
    > "This line contains the word cone."
    >
    > Set re = New RegExp
    > re.Pattern="\b(" & "One" & ")\b"
    > re.IgnoreCase=True
    > re.Global=True
    > strOutput=re.Replace(strInput,"<b>$1</b>")
    > Response.Write("<pre>" & strOutput & "</pre>")
    > Response.Write "<hr>"
    > Response.Write Highlight("One", strInput)%>
    > <SCRIPT LANGUAGE=vbscript RUNAT=Server>
    > Function Highlight(vFind, vSearch)
    > Dim RegEx
    > Set RegEx = New RegExp
    > RegEx.Pattern = "\b" & vFind & "\b"
    > RegEx.IgnoreCase = True
    > RegEx.Global = True
    > Highlight = RegEx.Replace(vSearch, "<b>$1</b>")
    > Set RegEx = Nothing
    > End Function
    > </SCRIPT>
    >
    >
     
    David Morgan, Aug 27, 2004
    #11
  12. David Morgan

    Steven Burn Guest

    Ah!..... hehe, my apologies

    --

    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!


    "David Morgan" <> wrote in message
    news:...
    > I don't mean to be rude Steve but you really have missed the point on this
    > one.
    >
    > Your last sentence is just wrong irrespective of my issue.
    >
    > If I say Replace("something", "something else", vbTextCompare) then
    > "Something" would be converted to "something else". What I am looking

    for,
    > (and if you read the other posts in this thread), that has now been
    > provided, is to replace "Something" with "Something else" and "something"
    > with "something else". In your world I would need two replace statements.
    >
    > I have only cited two variables here, "Something" and "something". There

    is
    > a large amount of variables regarding the case of the word being searched.
    > I want the replacement to be in the same case as the occurrence it

    replaced,
    > not in the case of the value being searched for.
    >
    > Hope this clears it up.
    >
    >
    > "Steven Burn" <> wrote in message
    > news:...
    > > Not really....... you wanted to know why "Something" was treated as
    > > "something"..??
    > >
    > > Although I know you can, I can't see any point in using RegExp for

    > something
    > > that can be done with the native Replace() function in one line of code.
    > >
    > > What your RegExp is doing with "RegExp.IgnoreCase = True" is essentially

    > the
    > > same as converting the query to lowercase before replacing the

    word/phrase
    > >
    > > --
    > >
    > > Regards
    > >
    > > Steven Burn
    > > Ur I.T. Mate Group
    > > www.it-mate.co.uk
    > >
    > > Keeping it FREE!
    > >
    > >
    > > "David Morgan" <> wrote in message
    > > news:...
    > > > Err... thanks, but you have missed my point.
    > > >
    > > > "Steven Burn" <> wrote in message
    > > > news:...
    > > > > Function Highlight(sFind, sSearch)
    > > > > Highlight = Replace(sSearch, sFind, "<span class=""highlight"">"

    &
    > > > sFind
    > > > > & "</span>")
    > > > > End Function
    > > > >
    > > > > --
    > > > >
    > > > > Regards
    > > > >
    > > > > Steven Burn
    > > > > Ur I.T. Mate Group
    > > > > www.it-mate.co.uk
    > > > >
    > > > > Keeping it FREE!
    > > > >
    > > > >
    > > > > "David Morgan" <> wrote in message
    > > > > news:#...
    > > > > > Hello
    > > > > >
    > > > > > I have a little function to highlight text if it exists.
    > > > > >
    > > > > > Function Highlight(vFind, vSearch)
    > > > > > Dim RegEx
    > > > > > Set RegEx = New RegExp
    > > > > > RegEx.Pattern = vFind
    > > > > > RegEx.IgnoreCase = True
    > > > > > Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" &

    > > vFind
    > > > &
    > > > > > "</span>")
    > > > > > Set RegEx = Nothing
    > > > > > End Function
    > > > > >
    > > > > > The only problem is, that if I search for "something", "something"
    > > > appears
    > > > > > highlighted when the string being searched actually contained
    > > > "Something".
    > > > > > This is not about case sensitive searching, this is about showing

    > the
    > > > > actual
    > > > > > match rather than what was being searched for. Hope that makes

    > sense.
    > > > > >
    > > > > > For example:
    > > > > >
    > > > > > Response.Write Highlight("something", "Have you seen Something

    > About
    > > > > > Mary?")
    > > > > >
    > > > > > Gives:
    > > > > >
    > > > > > Have you seen <span class="Highlight">something</span> About Mary?
    > > > > >
    > > > > > Whilst this is ok, it would be great if it actually highlighted

    the
    > > > match
    > > > > > rather than the 'find, i.e:
    > > > > >
    > > > > > Have you seen <span class="Highlight">Something</span> About Mary?
    > > > > >
    > > > > > I have looked in to the documentation a bit and it seems that to

    do
    > > what
    > > > I
    > > > > > want involves using the execute method and then hunting through

    the
    > > > > matches
    > > > > > collection. Does anyone have any sample code for this that I

    could
    > > > > > _borrow_?
    > > > > >
    > > > > > My function is nice and small and it would be a shame to have to

    go
    > > for
    > > > > > something bigger, (a.k.a. performance degradation), if a simple

    > change
    > > > can
    > > > > > be made to my function.
    > > > > >
    > > > > > I am open to suggestions on the JScript approach also as I notice

    > > these
    > > > > > special characters $0 - $9 although MSDN is somewhat lacking in

    > sample
    > > > > code
    > > > > > for a novice like me.
    > > > > >
    > > > > > MTIA
    > > > > >
    > > > > > David
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Steven Burn, Aug 27, 2004
    #12
  13. "David Morgan" <> wrote in message
    news:...
    [snip]
    > This still does not explain why $1 works, save me being on old docs :S


    Here's a link to the documentation for the RegExp.Replace method in
    VBScript.
    http://www.msdn.microsoft.com/library/en-us/script56/html/vsmthReplace.asp

    Note, in the "Remarks" section the following:

    "In addition, the Replace method can replace subexpressions in the
    pattern."

    HTH
    -Chris Hohmann
     
    Chris Hohmann, Aug 29, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pvii007
    Replies:
    2
    Views:
    1,117
    Patricia Shanahan
    May 21, 2005
  2. Robert Bevington

    Search in Dataview and Highlight Text

    Robert Bevington, Jun 6, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    114
    Robert Bevington
    Jun 6, 2008
  3. PiGei

    How to highlight words of a search

    PiGei, Mar 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    122
    Aaron [SQL Server MVP]
    Mar 16, 2005
  4. Daniel Di Vita
    Replies:
    2
    Views:
    208
    Anthony Jones
    Apr 21, 2006
  5. Craig Keightley
    Replies:
    10
    Views:
    261
    Craig Keightley
    Jun 29, 2005
Loading...

Share This Page