简体   繁体   中英

Get a number after a Specific Symbol in JQuery / Javascript

I have a String Like This:

"Dark Bronze - 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';
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", val;
val = parseFloat(str.slice(str.indexOf('$')));
alert('The value is ' + 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM