正则如何匹配出字符串中的特定字符?

比如一个字符串:


'/content/dam/sonystyle/products/color/silver/color_icon_grey_30x30.gif'


'/content/dam/sonystyle/products/color/'这里是固定的,后面在变

如果要取出silver这个字段?正则该怎么写?要兼容目前浏览器


米琪卡哇伊
浏览 6734回答 4
4回答

墨色风雨

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];}

桃花长相依

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

慕勒3428872

个人感觉没有必要使用正则let str = '/content/dam/sonystyle/products/color/silver/color_icon_grey_30x30.gif'let len = '/content/dam/sonystyle/products/color/'.lengthstr = 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'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript