I have an HTML element ("summary-payment") which contains the text: "Pay £100.00", for example.
I have managed to get the number 100 from this, as follows:
function getPaymentAmount(paymentAmount) {
var stringTotal = $("#paymentBtn").text();
var extractIntFromTotal = stringTotal.match(/\d+/)[0];
return extractIntFromTotal;
}
However, I'd like to get the decimal 100.00 back. I have tried adding onto the index but this doesn't seem to work. Can anyone please help? I am very new to javascript.
Thank you :)
One way to achieve this would be to amend your regular expression to include the decimals:
function getPaymentAmount(paymentAmount) {
return paymentAmount.match(/\d+[.,]?\d{2}?/)[0];
}
console.log(getPaymentAmount('Pay £100.00'));
console.log(getPaymentAmount('Pay £215'));
console.log(getPaymentAmount('Lorem ipsum $500.33 dolor sit'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With