Skip to content

Instantly share code, notes, and snippets.

@alexandreservian
Created January 30, 2020 16:47
Show Gist options
  • Save alexandreservian/eb5f9b619120ab3dc860bd60488d0a87 to your computer and use it in GitHub Desktop.
Save alexandreservian/eb5f9b619120ab3dc860bd60488d0a87 to your computer and use it in GitHub Desktop.
Método replace com função
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