Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
How do i replace letters with underscores but let the spaces stay in javascript

i am trying to replace the letters i get from the word with underscores but if there is any spaces in that word i do not want to replace it. im not sure how to do it. the current code i am using is this answerArray = [] function randomWord() {

    for (let i = 0; i < word.length; i++) {
        answerArray[i] = "_"
        answer.innerHTML = answerArray.join(" ")
    }
}

i've tried to look around in stackoverflow with my question but no one has it. someones suggested this

word.replace(/\w/gi, '_')

but it isnt working as i want.

You can check out the code here https://codepen.io/NoNameYet04/pen/OJxKYeV i have for the test change the word in "programming language" to "test test" so you can test it out

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Check if it's a space using an if statement like this:

if (word[i] !== ' '){
   answerArray[i] = '_';
}
else {
    answerArray[i] = ' ';
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I did a quick example below. With problems like this it's always better to try to break it down into small logical steps.

  • Go through each character

  • If it's a space then leave it

  • If it's not then replace with an _

    function randomWord(word) {
          let ret = ""; // return string
    
          for (let i = 0; i < word.length; i++) { // go through each character
              if(word[i] !== " "){ // if it's not a space then replace it
                ret += "_";
              } else if (word[i] === " "){ // if it's a space then we add to the return string
                ret += " ";
              }
    
          }
          return ret; //return or answer.innerHTML = ret;
    

    }

Call using: randomWord("hello there");

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe try this one by split the string to array and replace the word and finally join back.

The output for programming language will be ___________ ________

If you want to trigger the space between each character, use replace(/\w/g, '_ ') instead of replace(/\w/g, '_')

let word = 'programming language'
let answerArray = []
let newword = word.split(' ').forEach(i => {
  answerArray.push(i.replace(/\w/g, '_ '))
})
// answer.innerHTML = answerArray.join(" ")
console.log(answerArray.join(" "))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda