Link to home
Create AccountLog in
Avatar of niner
niner

asked on

Get the number value from string

How do I get the value of a number from a string in Javascript? Like with 'ABC12' I just want to return the 12.
ASKER CERTIFIED SOLUTION
Avatar of dakyd
dakyd

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of dakyd
dakyd

<Sorry, should've included this in the first post>

If you want to only get the first number/decimal in the string, you can use the following:

<script type="text/javascript">
var str = "ABC12g345";
str = parseFloat(str.replace(/^\D+/,""));
alert(str);
</script>

The regular expression will strip out any non-numbers in the beginning of the string, and then take the numbers left at the beginning.  Hope one of the two helps.