JS replaces all characters equal

Asked

Viewed 142 times

3

I’m trying to use the replace function to turn a letter to 0, but it doesn’t work when I want to test the letter inside a var.

var palavra = "abelha";
var letra = 'A';
palavra = palavra.replace(/(letra)/g, '0');

Some solution?

  • Try this one: https://stackoverflow.com/a/494046/6647038

1 answer

5


Can use new RegExp(padrão, flags), but you have to use the flag i also to ignore case sensitive (Do not differentiate between upper and lower case):

var palavra = "abelha";
var letra = 'A';
var re = new RegExp(letra, 'gi');
palavra = palavra.replace(re, '0');

console.log(palavra);

  • worked here, thanks!!! taking advantage here, how do I check that in the string everything is zero? in case the whole word gets 0 here, however I tried with if(!Nan(word)) and still does not show

  • @Paulohenriquerodriguesgrund In this case, ask another question

  • you want to see if it’s word = "0000000" ?

  • I’ll ask another question then, but I’ll have to wait a while to ask

  • I already answered a similar question, but I don’t think

  • But the thing is isNaN...

  • I try with isNaN but it doesn’t work, even if everything is 0

  • https://answall.com/questions/307757/verifica-se-uma-string-%C3%A9-only-compound-de-0

Show 3 more comments

Browser other questions tagged

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