Created
January 30, 2020 16:47
-
-
Save alexandreservian/eb5f9b619120ab3dc860bd60488d0a87 to your computer and use it in GitHub Desktop.
Método replace com função
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const texto = ` | |
Lista de jogos: | |
- Red dead redeption 2: R$ 180,00; | |
- The last of us 2: R$ 199,95; | |
- Resident Evil 2 remake: R$ 140,50; | |
`; | |
const regex = /(R\$)\s(\d*,\d*)/gmi; | |
const funcao = (match, p1, p2) => { | |
const real = parseFloat(p2.replace(/,/g,'.')); | |
const cotacao = 4.21; | |
const dolar = (real / cotacao).toFixed(2) | |
const result = dolar.toString().replace(/\./g,','); | |
return `US$ ${result}` | |
} | |
console.log(texto.replace(regex, funcao)) | |
/* | |
- Red dead redeption 2: US$ 42,76; | |
- The last of us 2: US$ 47,49; | |
- Resident Evil 2 remake: US$ 33,37; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment