I am trying to create a script that adds two strings that are pulled from a database. The script pulls the data correctly, but the strings contain a pound sign (£) - so consequently the numbers won't add together as they're effectively text strings.
var subtotal = "£25";
var shipping = "£5";
var total = subtotal + shipping;
How can I strip the pound signs from the code so that the numbers will add up?
You could remove all characters except digits and dots from the string using the string replace method with a regular expression, then call parseFloat on the result to cast it to a number.
const toNumber = n => parseFloat(n.replace(/[^.0-9]/g, ''));
var subtotal = "£25";
var shipping = "£5";
var total = toNumber(subtotal) + toNumber(shipping);
console.log(total);