Skip to content

Instantly share code, notes, and snippets.

@blessdyb
Created February 8, 2017 22:27
Show Gist options
  • Save blessdyb/895e33131fef6679ebbcf2b62e26b4ba to your computer and use it in GitHub Desktop.
Save blessdyb/895e33131fef6679ebbcf2b62e26b4ba to your computer and use it in GitHub Desktop.
Replace money from string
function replaceMoneyInString(disclaimer, new_price) {
const moneyRegexp = /\d+((,|'|\s)\d{3})*(\.\d*)?/;
let matches = disclaimer.match(moneyRegexp);
if (matches.length) {
let old_price_string = matches[0];
let splitter = old_price_string.replace(/\d/g, '').trim();
if (splitter.length === 1) {
let new_price_string = new_price.toString().replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1' + splitter);
return disclaimer.replace(old_price_string, new_price_string);
}
}
}
console.log(replaceMoneyInString('Enable after delivery for HK$46,600', 87000));
console.log(replaceMoneyInString("CHF 6'400 per l'attivazione dopo la consegna", 87000));
console.log(replaceMoneyInString("这个这个这个g这个这个一这个这个一共79.000RMB", 87000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment