'Using regex with javascript to money string transform
I have a money value as "R$ 2.200,00"
where my thousand separators are .
and my decimal separator is ,
.
I would like um regex to transform this string into a valid MySQL decimal number.
Possible values:
"R$ 23.000,20"
, "23.000,30"
, "R$ 1300,20"
, "R$ 100"
, "161,43256"
The results would be:
"23000.20"
, "23000.30"
, "1300.20"
, "100"
, "161.43256"
My attempt is below, but it does not work. I would like to accept only numbers and dot(.) for the results.
const str = "R$ 2.200,000";
const res = str
.replace(/R$/gi, "")
.replace(/./gi, "")
.replace(/,/gi, ".");
console.log(res);
Solution 1:[1]
With optimisations this seems to be working:
const possible = [
"R$ 23.000,20",
"23.000,30",
"R$ 1300,20",
"R$ 100 ",
"161,43256",
"R$ -23.000,20",
"-23.000,30",
"R$ -1300,20",
"R$ -100",
"-161,43256"
]
const rx = /[^-\d,]+/g // Match everything except digits, comma, dot and negative
const parseNumber = num =>
num
.replace(rx, '') // Filter out non numeric entities
.replace(',', '.') // Replacing comma with dot
possible.forEach(num => {
console.log(num, '\t->\t', parseNumber(num))
})
Solution 2:[2]
- this regEx replace/remove: (/[^\w\s]/gi) all special globaly signs ("$", ".", ","),
- next one give the digit part (/\D/g)
- and finally with string methods substring(,) place a ',' between the last two digits in the length of the string.
let input = "R$ 2.200,000";
let res = input.replace(/[^\w\s]/gi, '')
let str = res.replace(/\D/g,'');
let resStr = str.substring(0,str.length-2)+"."+str.substring(str.length-2);
console.log(resStr)
Solution 3:[3]
Edit: However this will not work if you have more than one thousand seperator, do not use it if you may have.
So, if you don't have to use regex, you can use replace function with strings too.
const str = "R$ 2.200,00";
const res = str
.replace('R$ ', "")
.replace('.', "")
.replace(',', ".")
console.log(res);
The code above will give you the format you want as a String. If you need Float, you can use parseFloat function.
console.log(parseFloat(res));
You can even use toFixed if you want absolute number of numbers after floating point
console.log(parseFloat(res).toFixed(5));
Solution 4:[4]
let input1 = "R$ 2.200,000";
let input2 = "23.000,30";
let input3 = "R$ 1300,20";
let input4 = "R$ 100";
let input5 = "161,43256";
const formatMoney = (inpStr) => {
let res = inpStr
res = res.replace(/[$R\s]/g, '');
let idx = res.indexOf('.')
res = res.replace(/,/, '.')
for (let i in res) {
if (res[i] === '.' && i !== i) {
res[i] = ','
}
}
return res
}
console.log(formatMoney(input1))
console.log(formatMoney(input2))
console.log(formatMoney(input3))
console.log(formatMoney(input4))
console.log(formatMoney(input5))
Solution 5:[5]
You could do that in two steps.
Step 1: remove characters
Substitute matches of the following regular expression with empty strings.
/(?:^R\$ +|[,.](?=.*[,.]))/gm
This will convert the strings
R$ 23.000,20
23.000,30
R$ 1300,20
R$ 100
161,43256
R$ 23.123.000,20
to
23000,20
23000,30
1300,20
100
161,43256
23123000,20
Javascript's regex engine performs the following steps.
(?: : begin a non-capture groups
^ : beginning of string
R\$ + : match 'R$' followed by 1+ spaces
| : or
[,.] : match ',' or '.'
(?= : begin positive lookahead
.* : match 0+ characters other than line terminators
[,.] : match ',' or '.'
) : end positive lookahead
) : end non-capture group
Step 2: convert comma, if present, to a period
Substitute matches of the following regular expression with a period.
/[,.]/gm
That would convert the above strings to the following.
23000.20
23000.30
1300.20
100
161.43256
23123000.20
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | |
Solution 4 | symlink |
Solution 5 | Cary Swoveland |