Link to home
Start Free TrialLog 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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.