Home > Software engineering >  How do I write a regular expression that brings specific characters?
How do I write a regular expression that brings specific characters?

Time:12-31

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);

CodePudding user response:

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','');
  • Related