當存在以下字串時,我想從內部提取特定字串。我要提取的字串是 uuid。
但是不知道怎么填正則運算式帶uuid。除了 uuid 前后的 '/' 和 '-' 之外,我該怎么寫?
const text = "hello_img/2021/12/27/uuid-c.jpg";
const reg = /\b\/u.*?-/g;
const matches = text.match(reg);
uj5u.com熱心網友回復:
在最后一次出現 / 之后獲取文本,這似乎是您想要的 uuid:
const url = "hello_img/2021/12/27/f189f4ae-af11-11e7-b252-186590cec0c1-helloworld.jpg";
console.log(url.split("/").pop());
輸出將是
f189f4ae-af11-11e7-b252-186590cec0c1-helloworld.jpg
要洗掉 .jpg,您可以使用
newUrl = url.replace('.jpg','');
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399087.html
標籤:javascript 正则表达式
