Posted By

stryju on 11/10/10


Tagged

regex javascript js parse number float String int


Versions (?)

get 1st float ( or int ) from given string


 / Published in: JavaScript
 

it parses given string and returns 1st number it encounters ( it will replace comas with dots so floats formatted as 12,345 could be 12.345 ( some countries use that ) )

then u can parse returned value as a number / float / int - do what You need with it ;-)

  1. (/[\d.]+/).exec((__PROVIDE_STRING_TO_PARSE__).replace(",", "."))
  2.  
  3. // if u want it to make even more dumb-proof ( like multiple dots/comas "within" the number ) use this:
  4.  
  5. (/[\d]+.?[\d]+/).exec((__PROVIDE_STRING_TO_PARSE__).replace(",", "."))

Report this snippet  

You need to login to post a comment.