开发者

Get a number after a Specific Symbol in JQuery / Javascript

开发者 https://www.devze.com 2023-02-20 10:38 出处:网络
I have a String Like This: \"Dark Bron开发者_如何学运维ze - add $120.00\" I need to pull the 120 into a float number variable.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消