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. 如何解決docker宿主機無法訪問容器中的服務(wù)?2. javascript - 如何使用nodejs 將.html 文件轉(zhuǎn)化成canvas3. angular.js - 輸入郵箱地址之后, 如何使其自動在末尾添加分號?4. 在mac下出現(xiàn)了兩個docker環(huán)境5. javascript - 后臺管理系統(tǒng)左側(cè)折疊導(dǎo)航欄數(shù)據(jù)較多,怎么樣直接通過搜索去定位到具體某一個菜單項位置,并展開當(dāng)前菜單6. docker-compose中volumes的問題7. python - Scrapy存在內(nèi)存泄漏的問題。8. java如何生成token?9. angular.js - $stateChangeSuccess事件在狀態(tài)跳轉(zhuǎn)的時候不執(zhí)行?10. python - 啟動Eric6時報錯:’qscintilla_zh_CN’ could not be loaded
