I have a String Like This:
"Dark Bron开发者_如何学运维ze - add $120.00"
I need to pull the 120 into a float number variable.
How would I do that?
var str = "Dark Bronze - add $120.00";
var val = str.match(/\$[0-9]*\.[0-9]*/)[0];
var f = Number(val.substring(1));
// (f is a number, do whatever you want with it)
var input = 'Dark Bronze - add $120.00',
toParse = input.substring(input.indexOf('$') + 1),
dollaz = parseFloat(toParse);
alert(dollaz);
Demo →
var str="Dark Bronze - add $120.00", val;
val = parseFloat(str.slice(str.indexOf('$')));
alert('The value is ' + val);
var str = 'Dark Bronze - add $120.00';
var pos = str.indexOf('$');
if (pos < 0) {
// string doesn't contain the $ symbol
}
else {
var val = parseFloat(str.substring(pos + 1));
// do something with val
}
var str = "Dark Bronze - add $120.00";
/*
[\$£¥€] - a character class with all currencies you are looking for
( - capture
\d+ - at least one digit
\. - a literal point character
\d{2} - exactly 2 digits
) - stop capturing
*/
var rxp = /[\$£¥€](\d+\.\d{2})/;
// the second member of the array returned by `match` contains the first capture
var strVal = str.match( rxp )[1];
var floatVal = parseFloat( strVal );
console.log( floatVal ); //120
精彩评论