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

166
Visualizações
How can I extract numbers between slashes in javascript

I have a strings with this pattern,

ver1.1/hello/12345/bar -> extract 12345
world/098767123/foo   -> extract 098767123
ver1.2344/foo/78687115/ -> extract 78687115

I used /\d+/g or /\d+/, but didn't have luck that always ver numbers come too. How can I extract numbers which does not have any char and between slashes?

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

0

Use a capturing group to capture only the numbers between slashes: \/(\d+)\/

console.log("ver1.1/hello/12345/bar".match(/\/(\d+)\//))

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a capture group for this, e.g.:

var regex = /\/(\d+)\//;
var string = "ver1.2344/foo/78687115/";
var match = string.match(regex);
if (match) {
    console.log(match[1]);
}

You can also use /(?:\/|^)(\d+)(?:\/|$)/ for numbers that happen at the beginning or end of the string. This just has two non-capturing groups that say "match / or the beginning/end"

about 4 years ago · Juan Pablo Isaza Relatório

0

You can add a map to the output of the match to strip out the slashes. The use of the global flag g also prevented duplicate results.

const regEx = /(?:\/)(\d+)(?:\/)/g;
const source = "ver1.1/hello/12345/bar";
const arrResults = Array.from(source.matchAll(regEx), m => m[1]);
console.log(arrResults)

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