Replace in ASP/VBS/SQL

Discussion in 'ASP General' started by Pret Orian, Jul 8, 2003.

  1. Pret Orian

    Pret Orian Guest

    I need to replace all occurancies of "is" in "This is a test (is), is,
    penis." with "<a href=is.htm>is</a>".
    The thing is, if I use a simple Replace function, the "is" in penis
    also gets replaced.
    I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
    done in ASP.
    TIA
    Pret Orian, Jul 8, 2003
    #1
    1. Advertising

  2. Pret Orian wrote:
    > I need to replace all occurancies of "is" in "This is a test (is), is,
    > penis." with "<a href=is.htm>is</a>".
    > The thing is, if I use a simple Replace function, the "is" in penis
    > also gets replaced.
    > I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
    > done in ASP.
    > TIA



    s1 = "This is his test (is), is, is."
    with new regexp
    .global = true
    .ignorecase = true
    .pattern = "\bis\b"
    s2 = .replace(s1,"<a href=is.htm>is</a>")
    end with

    msgbox s1 & vbcrlf & s2


    --
    Michael Harris
    Microsoft.MVP.Scripting
    Seattle WA US

    Technet Script Center
    http://www.microsoft.com/technet/scriptcenter/default.asp

    Microsoft® Windows®2000 Scripting Guide
    http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
    Michael Harris \(MVP\), Jul 9, 2003
    #2
    1. Advertising

  3. Pret Orian

    Pret Orian Guest

    (Pret Orian) wrote in message news:<>...
    > I need to replace all occurancies of "is" in "This is a test (is), is,
    > penis." with "<a href=is.htm>is</a>".
    > The thing is, if I use a simple Replace function, the "is" in penis
    > also gets replaced.
    > I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
    > done in ASP.
    > TIA


    I'm afraid I didn't explain my problem god enough.
    I want to replace "is" only if it is not a part of another word, so
    something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
    be positive matches, and "penis" should not.
    Pret Orian, Jul 9, 2003
    #3
  4. Pret Orian

    Joe Earnest Guest

    "Pret Orian" <> wrote in message
    news:...
    > I'm afraid I didn't explain my problem god enough.
    > I want to replace "is" only if it is not a part of another word, so
    > something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
    > be positive matches, and "penis" should not.


    Did you try Michael Harris' script? (He politely uses "his" in his example
    to demonstrate.)

    Joe Earnest
    Joe Earnest, Jul 9, 2003
    #4
  5. Pret Orian

    Pret Orian Guest

    >Did you try Michael Harris' script? (He politely uses "his" in his
    example
    >to demonstrate.)


    I did try the Michael's script and it did work indeed (thanx Michael).
    However, I'm having a problem with "is" word standing on the beginning
    of the string (or at the end). What would be a pattern that would
    exclude all the characters that are not a-zA-Z except if beeing on the
    beginning of the string
    For example:

    ..pattern = "\bis\b"

    Replaces: "this is something" "(is)" " This is." ...
    But leaves "is this something" untouched.

    Is there a way out?
    Where are the escape characters (like \b) documented?
    regards
    Pret Orian, Jul 9, 2003
    #5
  6. Pret Orian

    Joe Earnest Guest

    Hi,

    "Pret Orian" <> wrote in message
    news:...
    > I did try the Michael's script and it did work indeed (thanx Michael).
    > However, I'm having a problem with "is" word standing on the beginning
    > of the string (or at the end). What would be a pattern that would
    > exclude all the characters that are not a-zA-Z except if beeing on the
    > beginning of the string
    > For example:
    >
    > .pattern = "\bis\b"
    >
    > Replaces: "this is something" "(is)" " This is." ...
    > But leaves "is this something" untouched.
    >
    > Is there a way out?
    > Where are the escape characters (like \b) documented?
    > regards


    I'm not much at regex, and maybe some one can post a simple regex addition,
    but as a fallback, you could do the beginning and end issues with standard
    string manipulation, after using Michael's regex script. It's kludgy, but
    it works.

    s1= "is this something? it is, it is"

    ' Michael's regex script
    with new regexp
    .global = true
    .ignorecase = true
    .pattern = "\bis\b"
    s2 = .replace(s1,"<a href=is.htm>is</a>")
    end with

    'add
    if lcase(left(s2 & " ", 3)="is ") then _
    s2= "<a href=is.htm>is</a>" & mid(s2, 3)
    if lcase(right(s2, 3)=" is") then _
    s2= left(s2, len(s2) -2) & "<a href=is.htm>is</a>"

    msgbox s1 & vbcrlf & s2

    Joe Earnest
    Joe Earnest, Jul 9, 2003
    #6
  7. Pret Orian wrote:
    >> Did you try Michael Harris' script? (He politely uses "his" in his
    >> example to demonstrate.)

    >
    > I did try the Michael's script and it did work indeed (thanx Michael).
    > However, I'm having a problem with "is" word standing on the beginning
    > of the string (or at the end). What would be a pattern that would
    > exclude all the characters that are not a-zA-Z except if beeing on the
    > beginning of the string


    s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
    with new regexp
    .global = true
    .ignorecase = true
    .pattern = "\W(is)\W"
    s2 = .replace(s1,"<a href=is.htm>is</a>")
    end with

    msgbox s1 & vbcrlf & s2

    --
    Michael Harris
    Microsoft.MVP.Scripting
    Seattle WA US

    Technet Script Center
    http://www.microsoft.com/technet/scriptcenter/default.asp

    Microsoft® Windows®2000 Scripting Guide
    http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
    Michael Harris \(MVP\), Jul 10, 2003
    #7
  8. "Michael Harris (MVP)" <> wrote in message
    news:%...
    > What I actually meant was this...notice the $1 in the replacement

    string to
    > use the text that was matched in the replacement text...
    >
    > s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
    > with new regexp
    > .global = true
    > .ignorecase = true
    > .pattern = "\W(is)\W"
    > s2 = .replace(s1,"<a href=is.htm>$1</a>")
    > end with
    >
    > msgbox s1 & vbcrlf & s2
    >
    > --
    > Michael Harris
    > Microsoft.MVP.Scripting
    > Seattle WA US
    >
    > Technet Script Center
    > http://www.microsoft.com/technet/scriptcenter/default.asp
    >
    > Microsoft® Windows®2000 Scripting Guide
    >

    http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
    >


    Michael, your code consumes the surrounding "non-word" characters.
    Here's some code based on the OP's original pattern
    "[^a-zA-Z]is[^a-zA-Z]". I reduced the range since ignore case was in
    effect.

    <script language="VBScript" runat="SERVER">
    str = "Is this what his is_test is? - Is IS iS - yes, it is"
    with new regexp
    .global = true
    .ignorecase = true
    .pattern = "([^a-z])(is)([^a-z])"
    Response.Write "<br>VBScript: " & .replace(str,"$1<a
    href=is.htm>$2</a>$3")
    end with
    </script>
    <script language="JScript" runat="SERVER">
    Response.Write("<br>JScript: " + "Is this what his is_test is? - Is IS
    iS - yes, it is".replace(/([^a-z])(is)([^a-z])/gi,"$1<a
    href=is.htm>$2</a>$3"));
    </script>

    For the OP, here are links to regular expression syntax in VBScript and
    JScript respectively:
    http://msdn.microsoft.com/library/en-us/script56/html/vspropattern.asp
    http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.asp
    Chris Hohmann, Jul 10, 2003
    #8
    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. Brad

    Running a VBS file from ASP.NET

    Brad, Aug 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    509
  2. Alex
    Replies:
    1
    Views:
    2,612
    Alvin Bruney [MVP]
    Feb 16, 2004
  3. =?Utf-8?B?RGFuQw==?=

    Running Xcacls.vbs on ASP.net page fails

    =?Utf-8?B?RGFuQw==?=, Nov 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    709
    =?Utf-8?B?RGFuQw==?=
    Nov 15, 2005
  4. Nigel

    ASP.Net run VBS function

    Nigel, Aug 10, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    399
    =?Utf-8?B?U2l2YSBN?=
    Aug 10, 2007
  5. mike

    fire a VBS from ASP.NET

    mike, Aug 21, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    349
Loading...

Share This Page