Results 1 to 4 of 4

Thread: RegEx in VBScript

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140

    RegEx in VBScript

    With this ASP:

    Code:
    <% @Language="VBScript" %>
    <%
      Option Explicit
    
      Dim strTestOne
          strTestOne = "Blah"
      Dim strTestTwo
          strTestTwo = "Blah yadda yadda yadda Blah"
      Dim strResultOne, strResultTwo
    
      Dim objRegEx
      Set objRegEx = New regexp
    
      objRegEx.IgnoreCase = True
      objRegEx.Global = True
      objRegEx.Pattern = "\[url=""(.+)""\](.+)\[\/url\]"
    
      strResultOne = objRegEx.Replace(strTestOne, "<a href=""\1"">\2</a>")
      strResultTwo = objRegEx.Replace(strTestTwo, "<a href=""\1"">\2</a>")
    %>
    <html>
      <head>
        <title>VBScript RegEx Test</title>
      </head>
      <body>
        <p>Test string:  <%=strTestOne%></p>
        <p>Result string:  <%=strResultOne%></p>
        <p>Test string:  <%=strTestTwo%></p>
        <p>Result string:  <%=strResultTwo%></p>
        <p>Pattern:  <%=objRegEx.Pattern%></p>
      </body>
    </html>
    [/code]
    
    I get these reults:
    
    [code]
    <html>
      <head>
        <title>VBScript RegEx Test</title>
      </head>
      <body>
        <p>Test string:  [vrl="http://www.foo.com/bar.html"]Blah[/vrl]</p>
        <p>Result string:  <a href="\1">\2</a></p>
    
        <p>Test string:  [vrl="http://www.foo.com/bar.html"]Blah[/vrl] yadda yadda yadda [vrl="http://www.foo.com/bar.html"]Blah[/vrl]</p>
        <p>Result string:  <a href="\1">\2</a></p>
        <p>Pattern:  \[url="(.+)"\](.+)\[\/url\]</p>
      </body>
    </html>
    This isn't working at all. Any help?

    Note: "url" changed to "vrl" in some places in the response code to get past vBulletin.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Use $ instead of \.

    VB Code:
    1. Dim re As VBScript_RegExp_55.RegExp
    2.     Set re = New VBScript_RegExp_55.RegExp
    3.     re.Pattern = "^(\d{5})(\d{5})$"
    4.     Debug.Print re.Replace("1111122222", "$2$1")
    5.    
    6.     Set re = Nothing
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    $ works, thanks. That is contrary to the documentation at MSDN, but is more of what I would expect.

    Now, how do I get it to replace each instance and not the entire line?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Any ideas? I still can't get this RegEx to work consitently.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width