1

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

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

2 operations: .split("/")[3] followed by .split("-")[0] is equivalent of /^(?:[^\/]*\/){3}([^-]+)/
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]);

2 Comments

This is really close. Only issue is that it matches all the text before the number as well as the number. How do I omit that first group from the match?
That's why we use capture group for number. Modern Javascript uses lookbehind so you can use: /(?<=^(?:[^\/]*\/){3})[^-]+/
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.