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.

Basically, you can use a regular expression for this:


<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.

Couldn’t parseFloat() also work for this scenario?

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.

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? :stuck_out_tongue:

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));