str.replace with javascript only works once!

Asked

Viewed 280 times

-1

Good morning to all!! I wonder what I’m doing wrong in the code:

function () {
        var x = infoComentarioStrReplace.replace(" $$ ","*")//var resposta = infoComentarioStrReplace.replace("$", "#");
        console.log("=>"+x);
    }

this code takes the string and replaces all "$$" for * but when I launch the test value $$$ in the variable it only replaces a set of $$ that is, it returns me:

apenas um teste*$$

Can anyone tell me what’s wrong here??? thanks for your attention!!!

1 answer

0


The javascript replace replaces only the first occurrence of the string you pass as parameter.

a solution would be to do something like this:

while(infoComentarioStrReplace.indexOf(" $$") >= 0) {
    infoComentarioStrReplace = infoComentarioStrReplace.replace(" $$ ","*");
}
  • Thanks!! It worked out fine!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.