Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda