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

301
Visualizações
How can Javascript's regular expressions filter strings with multiple rules at the same time?

I need to process a piece of text into an array of words.

Delimiters between words are newlines, spaces, and various punctuation marks, and  .

The code I wrote was able to handle other cases, but not the   case.

Notice:I need to handle all cases within the same regex and cannot replace   with spaces.


This code doesn't go wrong, it just runs in chrome and the result is not the expected value.

In the generated word array, "break up test the words" is a value(wrong), I need it to be 5: [break,up,test,the,words](right)


my code:

<!DOCTYPE html><html><head>
<script>
window.onload = function(){
  var text = document.getElementById('text').textContent
  // &nbsp; of below regex doesn't work
  var word_array = text.split(/[ \t\n\r.?,"';:!(){}<>\/]|&nbsp;/)
  console.log(text)
  console.log(word_array)
}
</script>
</head><body>
<div id="text">this   is text,break&nbsp;up&nbsp;test&nbsp;the&nbsp;&nbsp;words!ok</div>
</body></html>

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

0

The issue is that the regex sees &nbsp as those exact characters. You want to use '\xa0' instead.

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