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

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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