Brazilian Currency Remove mask Regex

Viewed 924

Guys i have this mask that uses(jQuery MaskMoney plugin)

For example, if i type R$5.000,00 in the input and apply my function with this RegEx, it returns 5000.00, i want a Regex that returns 5000,00

$("#whatever").maskMoney({ prefix: "R$", decimal: ",", thousands: "." }); function RemoveMask(value) { var result = parseFloat(value.replace(/,/g, '.').replace(/[.](?=.*[.])/g, '').replace(/\R\$/g, '').replace(' ', '')); return result; } RemoveMask($("#whatever").val())

2 Answers

slice off the currency symbols then replace the others:

const str = "R$5.000,00";
const res = str.slice(2).replace(/\./g, "").replace(/,/g, ".");

Try this one.

function RemoveMask(value) {
  value = value.replace(/\./g, "");
  return /\d+,\d+/.exec(value)[0].toString();
}

console.log(RemoveMask("R$5.000,00"));
console.log(RemoveMask("R$5.000,02"));
console.log(RemoveMask("Lorem Ipsum R$5.000,05 test"));
console.log(RemoveMask("Lorem Ipsum R$5.000,00 test"));
console.log(RemoveMask("Lorem Ipsum R$500,00 test"));
console.log(RemoveMask("Lorem Ipsum R$0,05 test"));
console.log(RemoveMask("Lorem Ipsum R$0,50 test"));

Related