SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast mtran's Avatar
    Join Date
    Apr 2006
    Location
    San Jose, US
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to translate this Regular Expression

    I came across this javascript regular expression:
    g_start = start_gal.toString().replace(/\$|\,/g,'') * 1;

    I think it means: turn "start_gal" to a string and replace "$" or "," with ""
    Is it correct? Why *1?

    Thanks!

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's removing the $ and , from the string, which I'm assuming is operating on some kind of dollar amount. the *1 is probably trying to do some math with it, though I'm not sure if that multiplication would work out after doing a toString() on it...

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should work, it'd be more obvious to wrap the whole thing in parseFloat() though

  4. #4
    SitePoint Enthusiast mtran's Avatar
    Join Date
    Apr 2006
    Location
    San Jose, US
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!
    Multiply something by 1 -- is it redundant?

  5. #5
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, what's your input look like?

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    x * 1 is the fancy way to convert string to number. parseFloat(x) or Number(x) are more readable.


Bookmarks

Posting Permissions

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