4 回答
TA贡献1853条经验 获得超6个赞
function getKey() {
const reg = /\/content\/dam\/sonystyle\/products\/color\/([^\/]+)\/.+/;
const res = reg.exec('/content/dam/sonystyle/products/color/silver/color_icon_grey_30x30.gif');
return res && res[1];
}
TA贡献1860条经验 获得超8个赞
var a = "/content/dam/sonystyle/products/color/silver/color_icon_grey_30x30.gif"
var reg = /\/content\/dam\/sonystyle\/products\/color\/([^\/]+)/;
var v = reg.exec(a)[1];
console.log(v);
TA贡献1848条经验 获得超6个赞
个人感觉没有必要使用正则
let str = '/content/dam/sonystyle/products/color/silver/color_icon_grey_30x30.gif'
let len = '/content/dam/sonystyle/products/color/'.length
str = str.slice(len) // str = 'silver/color_icon_grey_30x30.gif'
str = str.split('/') // str = ['silver', 'color_icon_grey_30x30.gif']
str = str[0] // str = 'silver'
添加回答
举报