How to get the number from the middle of a string
Given the following string extract the number
'Buy for $199.99' // 199.99 'Gas Price $2.304 per gallon' // 2.304
We can solve this using slice and, parseFloat().
-
slicereturns the string from the starting index until the end of the string. -
parseFloatparses until it finds the non-numeric character.
parseFloat( 'Buy for $199.99'.slice(9) ) // 199.99 parseFloat( 'Gas Price $2.304 per gallon'.slice(11) ) // 2.304