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

117
Vistas
how to slice string if numbers starts in javascript

I have strings like these

total sales 234 rs
total cost 651 rs

and I want to get only

end result should look like this

total sales
total cost

how can i get that please help thanks

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

0

Assuming you always want to cut it off just before the first number, you could find the index of the first number:

const index = 'total sales 234 rs'.search(/\d/);

And then just get the substring:

substring(0, index - 1);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do something like this:

let str = 'total sales 234 rs';
let result = str.split(' ').slice(0,2).join(' ');
about 4 years ago · Juan Pablo Isaza Denunciar

0

Assuming that the string can contain more than two main words For eg Total goods cost 200 rs

then use the below function

function reducer(str) { // where str is the initial string
 const modifiedStr = str.split(' ').reduce((acc, substr) => {
  if(!parseInt(substr, 10) && substr !== 'rs') acc+=substr+' ';
  return acc;
 }, "");
 return modifiedStr.slice(0,modifiedStr.length - 2);
}
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