javascript - 正則的截取匹配問題求助
問題描述
srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
想截取從static開始的字符串,請問正則該如何寫?感謝
也就是staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png
另外由于url前半段可能會變動,所以最好還是用正則的好
問題解答
回答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']
這個問題的亮點:
相關(guān)文章:
1. java - 如何在Fragment中調(diào)用Activity的onNewIntent?2. python的文件讀寫問題?3. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現(xiàn) 4044. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat5. javascript - ionic1的插件如何遷移到ionic2的項目中6. python - 獲取到的數(shù)據(jù)生成新的mysql表7. css - 關(guān)于input標(biāo)簽disabled問題8. window下mysql中文亂碼怎么解決??9. javascript - h5上的手機(jī)號默認(rèn)沒有識別10. javascript - 圖片鏈接請求一直是pending狀態(tài),導(dǎo)致頁面崩潰,怎么解決?
