A first problem is that you stated function substituicaoRapida ( )
, that is, the function does not receive any parameter. And inside it, you always use the same text ("Good afternoon"), so no matter what you pass to the function, it will always use this text.
Therefore, its function should receive the texts as parameters, instead of always having a fixed text within it:
function substituicaoRapida(texto, antigo, novo) {
return texto.replace(antigo, novo);
}
console.log(substituicaoRapida('Boa tarde!', 'tarde', 'noite')); // Boa noite!
console.log(substituicaoRapida('Olá, usuário!', 'usuário', 'Ana')); // Olá, Ana!
Thus, the function receives the original text, the passage to be replaced and the new text. But it has two poréns:
The first is that the replace
is only done once. So if there is more than one occurrence of the word, only the first is replaced:
function substituicaoRapida(texto, antigo, novo) {
return texto.replace(antigo, novo);
}
console.log(substituicaoRapida('Boa tarde! Já é tarde.', 'tarde', 'noite')); // Boa noite! Já é tarde.
The above code prints "Good night! It’s late." as only the first occurrence of "late" was replaced by "night".
The other however - it is not clear if it is something that should be considered in your code (although not even the case of replacing only the first occurrence is specified, but anyway) - is that the replace
does not take into account whether the text to be replaced is actually part of a word or whether it should be only an entire word. Ex:
function substituicaoRapida(texto, antigo, novo) {
return texto.replace(antigo, novo);
}
console.log(substituicaoRapida('Entardeceu', 'tarde', 'noite')); // Ennoiteceu
The code above prints "Ennoiteceu".
Anyway, if you want to take into account the 2 cases above, just use a regular expression, using RegExp
:
function substituicaoRapida(texto, antigo, novo) {
return texto.replace(new RegExp(`\\b${antigo}\\b`, 'g'), novo);
}
console.log(substituicaoRapida('Boa tarde, é tarde. Entardeceu', 'tarde', 'noite')); // Boa noite, é noite. Entardeceu
The code above prints "Good night, it’s night. Dusk" (the two occurrences of "afternoon" were replaced by "night", and "Dusk" is not replaced by the word "late").
Basically, I used the shortcut \b
, that indicates a "boundary between words" (a position that contains an alphanumeric character before and a non-alphanumeric character after, or vice versa), so I guarantee that it will only take the word "afternoon", and ignore when it is part of another word (as in "Dusk"). Remembering that by being in a string, the character \
should be written as \\
. Here you can see more details about the \b
.
I also use the flag g
(the second parameter in the RegExp
), which causes all occurrences to be replaced (not only the first).
Important you [Dit] your post and explain the problem by describing what you tried and where is the current difficulty, preferably with a [mcve]. Studying the post available on this link can make a very positive difference in your use of the site: Stack Overflow Survival Guide in English
– Bacco