-1

In the URLs

https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e--wedding-vendors-wedding-receptions.jpg https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e.jpg

I'm trying to capture 5b026cdb06921e7ca5f7a24aff46512e in both of these strings. The string will always happen after the last slash, it will be a random assortment of letters and numbers, it may or may not have --randomtext appended, and it will have .jpg at the end.

I currently have ([^\/]+)$ to extract any string after the last slash, but would like to know how to capture everything before .jpg and --randomtext(if present). I will be using this in javascript.

saoirse
  • 41
  • 3
  • You should try something and post what you have tried and why it doesn't work, otherwise we don't know which part you're having difficulty with. Just writing an answer for you is not as helpful to you or others – Ruan Mendes Sep 16 '19 at 17:23
  • 1
    Try using a capturing group `^.*\/([a-z0-9]+).*\.jpg$` https://regex101.com/r/efXxYY/1 – The fourth bird Sep 16 '19 at 17:26
  • 3
    @wiktor-stribiżew The answer you linked to did not address the `--random-text` part of the question. – Ruan Mendes Sep 16 '19 at 17:28
  • Ok, probably [`([^\/]*?)(?:--[^\/]*)?\.\w+$`](https://regex101.com/r/zDp1Ay/2) will do it. What are the exact specs? – Wiktor Stribiżew Sep 16 '19 at 17:30
  • @Thefourthbird suggested something that works, sigh... I'd rather let the OP try a bit harder on their own – Ruan Mendes Sep 16 '19 at 17:30

3 Answers3

1

You can split by / and take the last part, and then replace anything after -- or .jpg from end with empty string

let arr = ["https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e--wedding-vendors-wedding-receptions.jpg","https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e.jpg"]

let getText = (url) =>{
  return url.split('/').pop().replace(/(--.*|\.jpg)$/g,'')
}

arr.forEach(url=> console.log(getText(url)))

If there are chances to have -- more than one time than instead of replacing you can simply match match(/^[a-z0-9]+/g) and take the first element from matched array

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

If what is after the last forward slash is a random assortment of letters and numbers a-z0-9, on option is to use a capturing group.

^.*\/([a-z0-9]+).*\.jpg$

In parts

  • ^ Start of string
  • .*\/ Match until including the last /
  • ([a-z0-9]+) Capture in group 1 matching 1+ chars a-z or digits 0-9
  • .* Match any char except a newline 0+ times
  • \.jpg Match .jpg
  • $ End of string

Regex demo

const regex = /^.*\/([a-z0-9]+).*\.jpg$/;
["https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e--wedding-vendors-wedding-receptions.jpg",
  "https://image/4x/c1/abc/5b026cdb06921e7ca5f7a24aff46512e.jpg"
].forEach(s => console.log(s.match(regex)[1]));
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Thanks for the thorough breakdown. Super new to regex so this is a really good starting point for me – saoirse Sep 16 '19 at 22:12
  • 1
    In my previous attempts, I didn't have a good understanding of capturing groups, so I was applying parenthesis left and right without knowing what was happening. I really appreciate the explanation – saoirse Sep 16 '19 at 22:29
0

Use:

([^\/]*?)(?:--.*)?\.jpg$

and your desired match will be in $1

https://regex101.com/r/gZ9kSi/1

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 1
    `([^\/]*?)(?:--.*)?\.jpg$` is [wrong](https://regex101.com/r/gZ9kSi/2). [My suggestion](https://stackoverflow.com/questions/57961611/regex-to-extract-last-part-of-url-and-before-another-character?noredirect=1#comment102335028_57961611) is a fix for your regex. – Wiktor Stribiżew Sep 16 '19 at 18:01