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

200
Visualizações
remove extra spaces in string in javascript

I have a text and after deleting special characters (!@#$%^&*()-=+`";:'><.?/) and show just letters and numbers (and float numbers like 23.4 ) it returns some extra space

    const input : 'this is a signal , entry : 24.30 and side is short';

    const text = input.replace(/\.(?!\d)|[^\w.]/g, " ").toUpperCase();

    console.log(text.split(" "))

the output :

[
  'THIS',   'IS',    'A',
  'SIGNAL', '',      '',
  '',       'ENTRY', '',
  '',       '24.30', 'AND',
  'SIDE',   'IS',    'SHORT'
]

but I want to be this :

[
  'THIS',   'IS',    'A',
  'SIGNAL', 'ENTRY', '24.30',  
  'AND',    'SIDE',   'IS',     
  'SHORT'
]

And when I replace spaces and enters with empty string , returns this :

[ 'THISISASIGNALENTRY24.30ANDSIDEISSHORT' ]

what is the problem of my code?

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

0

Instead of replacing, consider matching all the sorts of characters you want to produce the array of words. It looks like you want something like:

const input = 'this is a signal , entry : 24.30 and side is short';
const matches = input.toUpperCase().match(/[\w.]+/g);
console.log(matches);

about 4 years ago · Juan Pablo Isaza Relatório

0

The second parameter in the replace method needs to be an empty string and not a space as you have it in your code.

Just do:

...
const text = input.replace(/\.(?!\d)|[^\w.]/g, "").toUpperCase();
...
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