NEWBIE: replace punctuation

Discussion in 'Javascript' started by Erich93063, May 4, 2007.

  1. Erich93063

    Erich93063 Guest

    Hi sorry this is probably the easiest answer ever but how do I replace
    all punctuation in a variable. I also in the same function want to
    replace all spaces with underscores.

    THANKS
    Erich93063, May 4, 2007
    #1
    1. Advertising

  2. Erich93063 wrote:
    > Hi sorry this is probably the easiest answer ever but how do I replace
    > all punctuation in a variable. I also in the same function want to
    > replace all spaces with underscores.



    try:

    variable=variable.replace(/[^\w-]/g,'').replace(/\s/g,'_')
    scripts.contact, May 4, 2007
    #2
    1. Advertising

  3. Erich93063

    Erich93063 Guest

    On May 4, 12:48 pm, "scripts.contact" <>
    wrote:
    > Erich93063 wrote:
    > > Hi sorry this is probably the easiest answer ever but how do I replace
    > > all punctuation in a variable. I also in the same function want to
    > > replace all spaces with underscores.

    >
    > try:
    >
    > variable=variable.replace(/[^\w-]/g,'').replace(/\s/g,'_')


    Thanks.

    Ok so I tried that and it did replace the punctuation but the spaces
    just got removed instead of being replaced with "_"
    Erich93063, May 4, 2007
    #3
  4. Erich93063

    Erich93063 Guest

    On May 4, 12:59 pm, Erich93063 <> wrote:
    > On May 4, 12:48 pm, "scripts.contact" <>
    > wrote:
    >
    > > Erich93063 wrote:
    > > > Hi sorry this is probably the easiest answer ever but how do I replace
    > > > all punctuation in a variable. I also in the same function want to
    > > > replace all spaces with underscores.

    >
    > > try:

    >
    > > variable=variable.replace(/[^\w-]/g,'').replace(/\s/g,'_')

    >
    > Thanks.
    >
    > Ok so I tried that and it did replace the punctuation but the spaces
    > just got removed instead of being replaced with "_"


    After messing around with it, this ended up working:

    cboModel.value.replace(/ /g,'_').replace(/[^\w-]/g,'')

    THANKS
    Erich93063, May 4, 2007
    #4
    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. Chris Leffer

    Regular expression for punctuation

    Chris Leffer, Jul 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    427
    Chris Leffer
    Jul 9, 2003
  2. Chris R. Timmons

    Re: Regular expression for punctuation

    Chris R. Timmons, Jul 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    476
    Chris R. Timmons
    Jul 10, 2003
  3. DBLWizard
    Replies:
    10
    Views:
    744
    Brock Allen
    Apr 2, 2005
  4. dew

    Stripping out punctuation marks

    dew, Feb 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    420
    Nathan Sokalski
    Feb 7, 2006
  5. dfhLASST

    Remove punctuation from String?

    dfhLASST, Nov 11, 2004, in forum: Java
    Replies:
    4
    Views:
    55,331
    Woebegone
    Nov 11, 2004
Loading...

Share This Page