Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Capturar después de la enésima aparición de una cadena usando expresiones regulares

Mi cadena de prueba:

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

Estoy tratando de capturar el número en la cadena. Las constantes en esa cadena son el número siempre precedido por 3 / y seguido por un - .

Soy un novato de expresiones regulares y estoy luchando con esto. Improvisé (\/)(.*?)(-) y luego pensé que podría obtener el último mediante programación, pero realmente me gustaría entender mejor las expresiones regulares y me encantaría que alguien pudiera mostrarme las expresiones regulares para obtener la última aparición. de números entre / y - .

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

No use expresiones regulares si es posible, le recomiendo que lea: https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ publicación de blog

A su pregunta, es más fácil, más rápido y más a prueba de balas obtenerlo usando divisiones

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

Espero eso ayude

about 4 years ago · Santiago Gelvez Report

0

Puede usar esta expresión regular con un grupo de captura:

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

O en los navegadores modernos puede usar la aserción lookbehind:

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

Demostración RegEx 1

Demostración RegEx 2

Código RegEx:

  • ^ : Inicio
  • (?:[^\/]*\/){3} : coincide con 0 o más caracteres que no sean / seguidos de / . Repite este grupo 3 veces
  • ([^-]+) : Partido 1+ de caracteres sin guión

Código:

 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 Report

0

Usar

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

Ver prueba de expresiones regulares .

EXPLICACIÓN

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!