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 - angularjs如何傳遞id給另一個視圖 根據id獲取json數據?2. java - HashSet<int> 為何有錯誤?3. mysql - 記得以前在哪里看過一個估算時間的網站4. 使用text-shadow可以給圖片加陰影嗎?5. docker start -a dockername 老是卡住,什么情況?6. nginx啟用gzip壓縮后,文件尺寸無變化.7. java - StringBuffer轉成String,可以不同過tostring,而是通過+“”的方式轉換嗎?8. 數據庫無法進入9. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?10. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失??!
