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

264
Visualizações
Typescript Replace a string between two characters within a string

I am new to typescript, I want to strip the timestamp from a file name. Filenames can be like FileName_20210210050124592.xml or FileName_20210210050124592_2.xml I only want to remove the timestamp (I want to keep the _2 in the second filename) This is what I have tried so far which works,

let fileName: string = "FileName_20210210050124592.xml";
let newFileName: string;
if((fileName.match(/_/g)||[]).length>1){
    newFileName = fileName.replace(/_.*_/, '_');
}
else{
    newFileName = fileName.replace(/_.*\./, '.');
}
console.log(newFileName);

Is there a better way to do it or can I improve this code?

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

0

You need to decide how you want to differentiate a timestamp from a file number.

The example below assumes that a timestamp will have at least 12 digits and that a file number will have less than that:

let fileName = "FileName_20210210050124592.xml";
let newFileName = fileName.replace(/_\d{12}\d*/, '');
console.log(newFileName);

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