SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast gunbound_boy's Avatar
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript to extract numbers out of a string

    Hello all,

    I need help with some simple programming.

    I need a script to extract numbers out of a string.
    For eg, this string contains the following fields:

    $TOTAL (variable) = US$2.40 (string)

    I need to remove the US$ so that when i submit the form it only passes 2.40 into the output.

    Will appreciate any advise, cause I am not really good at this.

    Thks in advance.
    SaintVida.com
    An online clothing store for the image conscious
    men and women of the modern era.

    http://www.saintvida.com.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, you can use a regular expression for this:

    Code:
    <script>
    a = "foo 12.34 bar 56 baz 78.90";
    numbers = a.match(/\d+\.?\d*/g)
    alert(numbers)
    </script>
    It this snippet, "numbers" is an array containing all numbers found in given string.

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Couldn't parseFloat() also work for this scenario?

  4. #4
    SitePoint Enthusiast gunbound_boy's Avatar
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thks for the reply.

    Now I realised i got another problem.

    I need to retrieve the number that is stored in php.
    ie. <?php $TOTAL= US$2.40 ?>...

    Then I need to send this number outside of the php brackets to another destination via a form.

    I hope you guys get what I mean...

    Thks.
    SaintVida.com
    An online clothing store for the image conscious
    men and women of the modern era.

    http://www.saintvida.com.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Couldn't parseFloat() also work for this scenario?
    If you paste the following line into any browser's address bar, what do you get?

    javascript:alert(parseFloat("US$2.40"));

    You on a hiking trip in the Himilayas or something?

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also do this:

    var str = "US$2.40";
    str = str.substring(3); //gets the substring from index position 3 to the end
    str = Number(str); //converts to a number
    alert(typeof(str));


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
  •