javascript - 正則的截取匹配問(wèn)題求助
問(wèn)題描述
srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
想截取從static開(kāi)始的字符串,請(qǐng)問(wèn)正則該如何寫(xiě)?感謝
也就是staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
另外由于url前半段可能會(huì)變動(dòng),所以最好還是用正則的好
問(wèn)題解答
回答1:var str=’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’;alert(str.replace(/^.*?(static.*?)$/ig, ’$1’));回答2:
split(’static’)[1] 這樣的嗎? 還是必須用正則?
回答3:str.slice(str.search(/static/));
回答4:正則應(yīng)該用 static.* 就可以,下面是參考代碼
const regex = /static.*/g;const str = `srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png`;let m;while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) {regex.lastIndex++; }// The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => {console.log(`Found match, group ${groupIndex}: ${match}`); });回答5:
’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’.split(’static’)[1]
回答6:’srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png’.match(/static.*/)// Output: [ 'staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png']
這個(gè)問(wèn)題的亮點(diǎn):
相關(guān)文章:
1. 致命錯(cuò)誤: Class ’appfacadeTest’ not found2. html5 - 如何實(shí)現(xiàn)帶陰影的不規(guī)則容器?3. objective-c - iOS開(kāi)發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開(kāi)發(fā)的一個(gè)app?4. css - 移動(dòng)端字體設(shè)置問(wèn)題5. python - 管道符和ssh傳文件6. javascript - 循環(huán)嵌套多個(gè)promise應(yīng)該如何實(shí)現(xiàn)?7. mysql優(yōu)化 - 關(guān)于mysql分區(qū)8. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒(méi)有反應(yīng)9. 前端 - IE9 css兼容問(wèn)題10. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤(pán)不彈出
