Handling spaces, commas, numbers and currency in a string

Discussion in 'Javascript' started by Andrew Poulos, Sep 17, 2005.

  1. I'm a novice with using regex. Could someone either direct me to where I
    can find out more about it or help with the things I need to do:

    - strip any leading and trailing spaces

    - reduce multiple spaces to single spaces

    - get the first number (integer) as a string from a string (with commas
    being ignored) eg. 'ab1,234c5de' returns '1234'

    - get the first number (real) as a string from a string (with commas
    being ignored) eg. 'ab1,234.567c89de' returns '1234.567'

    And the last one I need is a bit more difficult to describe. It's
    getting a currency type value which I'm defining as a integer followed
    by a decimal point which is followed by a maximum 2 digit integer. If
    the value does not have a decimal point, two zeros and a point are added
    to the value. If there's a decimal point, two zeros are added. If
    there's a point and a single digit integer a trailing zero is added. If
    there are more than two digits after the decimal point they are ignored.
    If there's no integer before the decimal point a zero is added. Examples:

    aaa$123bbb returns 123.00
    aaa1,234.5888585bbb returns 1234.58
    aaa.5bbb returns 0.50
    aaa3. returns 3.40
    aaaaa returns 0.00

    I've being trying to do it using replace, working one condition at a
    time, but each condition adds several lines and I'm only part way
    through and the script is unyielding.

    Andrew Poulos
    Andrew Poulos, Sep 17, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.