#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Australia
    Posts
    9
    Rep Power
    0

    SIMPLE regex not working + some questions


    Hi all,

    Noob regexer here. Given the following JS (with regex)...

    description = trim(description).replace(/ +/g , " ");

    ...in the trimmed version of "description", this is simply a global replace of double-space with single space, correct?

    I just need confirmation because something isn't working and I want to rule out my interpretation of this regex.

    * Can someone also tell me why everything between / and / doesn't need to be in double-quotes?

    * Also, can someone tell me the significance of the +

    Thanks all. I am making my way through some of the resources, but if I can get help with this "needs to be fixed now" issue in the meantime, that would be great. Thanks!


    Cheers,

    Stanbridge
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    Code:
    description = trim(description).replace(/  +/g , " ");
    [code] tags. It's important to use them. In fact in this case they're practically necessary.

    Yes, it replaces two or more spaces. The /.../ is because Javascript allows a specific syntax for regular expressions (quotes use strings and regexes use /s) while the + means one or more of the previous thing (forming literally "a space followed by one or more spaces").
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Australia
    Posts
    9
    Rep Power
    0
    Thankyou very much!

    And yes, good point, will use CODE tags moving forward.

    Cheers

IMN logo majestic logo threadwatch logo seochat tools logo