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

323
Visualizações
modify user input in javascript

I need to take a user input, in this case as a search word. But when sending this input to an API, the string must not contain any spaces. I tried .trim but this seems to only remove any spaces before and/or after the string. Essentially, I need to force any amount of words other than one together, any thoughts on how this could be achieved would be greatly appreciated.

function getSearch(){
var welcomeMsg = document.getElementById('result');
var search_term = document.getElementById('floatingInput');

<input type="text" class="form-control" id="floatingInput">
  <input type="button" value="click">
  <label for="floatingInput">Song title / artist name</label>
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use String.prototype.replace() for this, for example:

> 'foo bar'.replace(' ', '')
'foobar'

If there are expected to be many spaces, you can use a global regular expression:

> 'foo bar baz otherfoo'.replace(/ /g, '')
'foobarbazotherfoo'

Finally, if you still want to see the word boundaries as suggested by @JoeKincognito, you can update the second argument from an empty space '' to the separator of your choice, for example:

> 'foo bar baz otherfoo'.replace(/ /g, '-')
'foo-bar-baz-otherfoo'

This will allow you to separate the search terms later if necessary, and will allow you to differentiate between users searching for e.g., butter and fingers from users searching for butterfingers

over 4 years ago · Santiago Trujillo Relatório

0

string.replace() method supports regex expression:

xxx.replace(/\s+/g, '') //xxx is an example string.
\s: matches any whitespace symbol
over 4 years ago · Santiago Trujillo 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