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

149
Vistas
JQuery - remove text after character, keep string comma seperated

I have a string:

XXX - Test Text 1, OOO - Test Text 2, Dummy

What I want to do is return this string as:

XXX,OOO,Dummy

So I need to remove everything (and including the dash) but keep the string comma separated. This string could have up to say 10 variables.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This has nothing to do with jQuery, it is just native JavaScript. What you could do is split the string for every comma. Then split every part on the - character and join all the parts together:

parsedString = "XXX - Test Text 1, OOO - Test Text 2, Dummy".split(',')
  .map(part => part.split('-')[0].trim())
  .join(',')

console.log(parsedString);

about 4 years ago · Juan Pablo Isaza Denunciar

0

1) You can first split the string which will returns an array of strings

str.split(",")

2) use reduce to only get the initial string of all strings in an array that is returned from the 1st step

3) then, join them with ,

const str = "XXX - Test Text 1, OOO - Test Text 2, Dummy";
const result = str
  .split(",")
  .reduce((acc, curr) => {
    let match = curr.match(/[a-z]+/i);
    if (match) acc.push(match[0]);
    return acc;
  }, [])
  .join(",");

console.log(result);

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