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.
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
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 az or digits 0-9 .*
Match any char except a newline 0+ times \\.jpg
Match .jpg
$
End of string 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.