"R$ 2.200,00"
천 단위 구분 기호가 .
있고 소수점 구분 기호가있는 위치에 돈 가치 가 ,
있습니다.
정규식 이이 문자열을 유효한 MySQL 십진수로 변환하고 싶습니다.
가능한 값 :
"R$ 23.000,20"
, "23.000,30"
, "R$ 1300,20"
, "R$ 100"
,"161,43256"
결과는 다음과 같습니다.
"23000.20"
, "23000.30"
, "1300.20"
, "100"
,"161.43256"
내 시도는 아래에 있지만 작동하지 않습니다. 결과는 숫자와 점 (.) 만 받고 싶습니다.
const str = "R$ 2.200,000";
const res = str
.replace(/R$/gi, "")
.replace(/./gi, "")
.replace(/,/gi, ".");
console.log(res);
최적화를 통해 이것은 작동하는 것 같습니다.
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 and negative
const parseNumber = num =>
num
.replace(rx, '') // Filter digits and comma
.replace(',', '.') // Replacing comma with dot
possible.forEach(num => {
console.log(num, '\t->\t', parseNumber(num))
})
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다