I have this regex to extract monetary values from strings:
/[$R]\s*(\d*,?\d+)?\b/g
It is working fine. The new problem is, sometimes I will have two monetary values in the same string. For example:
Your purchase of R$ 447,71 was accepted. Your new balance is R$ 20,31.
So, how could I do in order to extract both values or at least extract first one? Because my regex extracted the last one...
The function:
treatMonetary(monetry: string) {
let resultString = '';
var res = monetry.match(/[$R]\s*(\d*,?\d+)?\b/g);
res?.forEach(element => {
if (element.includes("$")) {
resultString = element?.replace('$', '')?.replace(" ", "")?.replace("R", "")?.replace(',', '.');
}
});
return resultString;
}