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

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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