I want to extract a specific string from the inside when the following string exists. The string I want to extract is uuid.
But I don't know how to fill out the regular expression to bring uuid. How can I write it except for '/' and '-' before and after uuid?
const text = "hello_img/2021/12/27/uuid-c.jpg";
const reg = /\b\/u.*?-/g;
const matches = text.match(reg);
console.log(matches);
Take text after last occurence of /, that seems to be th uuid that you want:
const url = "hello_img/2021/12/27/f189f4ae-af11-11e7-b252-186590cec0c1-helloworld.jpg";
console.log(url.split("/").pop());
output will be
f189f4ae-af11-11e7-b252-186590cec0c1-helloworld.jpg
to remove .jpg, you can use
newUrl = url.replace('.jpg','');
If you want to match the actual format of the uuid after the last occurrence of the / then you can match the / and capture the format in capture group 1 followed by matching any char except / till the end of the strin.
\/\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b[^\/\r\n]*$
const text = "hello_img/2021/12/27/f189f4ae-af11-11e7-b252-186590cec0c1-helloworld.jpg";
const reg = /\/\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b[^\/\r\n]*$/;
const m = text.match(reg);
if (m) {
console.log(m[1]);
}