RegExp Pattern Eluding Me

Discussion in 'ASP General' started by Colin Steadman, Nov 26, 2003.

  1. I'm trying to setup a function that will remove any
    character from a string which is NOT either a space, a-z
    or 0-9. I'm using a Regular Expression to do this.

    I'm almost there but I cant get the pattern right. After
    trying all sorts of crazy patterns the closest I've come
    is with this simplistic attempt:


    Function ripOutSpecialCharacters(strComment)
    Set re = New RegExp
    re.pattern = "\W"
    re.global = True
    ripOutSpecialCharacters = re.replace(strComment, " ")
    Set re = Nothing
    End Function


    This is almost what I want, but if I run it on a string
    like this -

    Its that special blend and
    roast.

    I end up with this -

    Its that special blend and roast.
    ^^ ie two spaces here.

    I'm really struggling to get the pattern right, can anyone
    offer any help on this?

    TIA,

    Colin
    Colin Steadman, Nov 26, 2003
    #1
    1. Advertising

  2. I think I've done enough to satisfy myself. My function
    now looks like this -

    Function ripOutSpecialCharacters(strComment)
    Set re = New RegExp
    re.pattern = "\r\n"
    re.global = True
    ripOutSpecialCharacters = re.replace(strComment, "")
    Set re = Nothing
    End Function

    This is dealing with the carriage returns I was trying to
    get rid of, although I'm not sure why \r cant be used on
    its own?

    Colin
    Colin Steadman, Nov 26, 2003
    #2
    1. Advertising

  3. Colin Steadman

    keith Guest

    If you truly want all characters "which [are] NOT either a
    space, a-z or 0-9"

    Try

    \w Matches any word character including underscore.
    Equivalent to "[A-Za-z0-9_]".

    OR

    \W Matches any non-word character. Equivalent to "[^A-Za-
    z0-9_]".

    Although you may have to add, or remove, something if you
    want to exclude underscores, but it is more all
    encompassing than just replacing \r and \n.



    >-----Original Message-----
    >I think I've done enough to satisfy myself. My function
    >now looks like this -
    >
    >Function ripOutSpecialCharacters(strComment)
    > Set re = New RegExp
    > re.pattern = "\r\n"
    > re.global = True
    > ripOutSpecialCharacters = re.replace(strComment, "")
    > Set re = Nothing
    >End Function
    >
    >This is dealing with the carriage returns I was trying to
    >get rid of, although I'm not sure why \r cant be used on
    >its own?
    >
    >Colin
    >.
    >
    keith, Nov 26, 2003
    #3
    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. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    466
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    342
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    185
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    170
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page