An JS string replacement problem

wrong dollar sign in the question editor-^-

there is a code for currency formatting in the "JavaScript regular Table Kill Mini Book":

function format (num) {
return num.toFixed(2).replace(/\B(?=(\d{3})+\b)/g, ",").replace(/^/, "$$ ");
};
console.log( format(1888) );
// => "$ 1,888.00"

Why do I use two dollar signs here,
instead of one dollar sign,
and I am also correct to use a dollar sign in Chrome?

Mar.21,2021

browser problem, not IE and IE


can you test it in multiple browsers?


has nothing to do with the browser.
this is because the $symbol is meaningful in substring matching, which represents the substring in (). You can refer to ide/Regular_Expressions-sharp%E4%BD%BF%E7%94%A8%E6%8B%AC%E5%8F%B7%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%B9%E9%85%8D" rel=" nofollow noreferrer "> MDN substring matching instructions , The scene of your question does not reflect
for another scene: use the rule to fill in 1 in front of a string of numbers with only a fractional part, and add the symbol $ at the beginning and / month :

at the end.
('.33').replace(/^(\.\d+)($)/, '$$1$1/') //$$1

('.33').replace(/^(\.\d+)($)/, '$1$1/') // .33.33/
Menu