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:正則應該用 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']
這個問題的亮點:
相關文章:
1. angular.js - Angular路由和express路由的組合使用問題2. 網絡傳輸協議 - 以下三種下載方式有什么不同?如何用python模擬下載器下載?3. java 排序的問題4. 表單提交驗證,沒反應,求老師指點5. 我的html頁面一提交,網頁便顯示出了我的php代碼,求問是什么原因?6. 如何修改phpstudy的phpmyadmin放到其他地方7. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?8. tp6表單令牌9. node.js - gulp文件監聽的問題10. php - mysql中,作為主鍵的字段,用int類型,是不是比用char類型的效率更高?
