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. javascript - 手機點擊input時,button會被頂上去?求解決!!!2. 百度地圖api - Android 百度地圖 集成了定位,導航 相互的jar包有沖突?3. python - django 按日歸檔統計訂單求解4. 網頁爬蟲 - python爬蟲用BeautifulSoup爬取<s>元素并寫入字典,但某些div下沒有這一元素,導致自動寫入下一條,如何解決?5. javascript - vue-mint UI - icon在哪里有文檔?6. HTML5禁止img預覽該怎么解決?7. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。8. html5 - 表單無法屏蔽自動填充 autocomplete=off9. html - 請教一個前端css問題。10. 請教一個python字符串處理的問題?
