JSFiddle - React, Tailwind, and code Playground

JavaScript

function format2(n, currency) {
    return currency + " " + n.toFixed(2).replace(/^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d{1,2})?$/g, "$1,");
}
alert(format2(1, 'R$'));