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

150
Visualizações
Capturing after the nth occurrence of a string using regex

My test string:

/custom-heads/food-drinks/51374-easter-bunny-cake

I am trying to capture the number in the string. The constants in that string are the the number is always preceded by 3 /'s and followed by a -.

I am a regex noob and am struggling with this. I cobbled together (\/)(.*?)(-) and then figured I could get the last one programmatically, but I would really like to understand regex better and would love if someone could show me the regex to get the last occurrence of numbers between / and -.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Don't use regexes if possible, i reccomend you to read - https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ blog post

To your question, its easier, faster, more bullet proof to get it using splits

const articleName = "/custom-heads/food-drinks/51374-easter-bunny-cake".split("/")[3]
// '51374-easter-bunny-cake'

const articleId = articleName.split("-")[0]

// '51374'

hope it helps

about 4 years ago · Santiago Gelvez Relatório

0

You may use this regex with a capture group:

^(?:[^\/]*\/){3}([^-]+)

Or in modern browsers you can use lookbehind assertion:

/(?<=^(?:[^\/]*\/){3})[^-]+/

RegEx Demo 1

RegEx Demo 2

RegEx Code:

  • ^: Start
  • (?:[^\/]*\/){3}: Match 0 or more non-/ characters followed by a /. Repeat this group 3 times
  • ([^-]+): Match 1+ of non-hyphen characters

Code:

const s = `/custom-heads/food-drinks/51374-easter-bunny-cake`;

const re = /^(?:[^\/]*\/){3}([^-]+)/;

console.log (s.match(re)[1]);

about 4 years ago · Santiago Gelvez Relatório

0

Use

const str = `/custom-heads/food-drinks/51374-easter-bunny-cake`
const p = /(?:\/[^\/]*){2}\/(\d+)-/
console.log(str.match(p)?.[1])

See regex proof.

EXPLANATION

Non-capturing group (?:\/[^\/]*){2}
{2} matches the previous token exactly 2 times
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
  Match a single character not present in the list below [^\/]
  * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
  \/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
1st Capturing Group (\d+)
  \d matches a digit (equivalent to [0-9])
  + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
about 4 years ago · Santiago Gelvez 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