成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript - 正則的截取匹配問(wèn)題求助

瀏覽:176日期:2023-03-10 13:59:07

問(wèn)題描述

srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png

想截取從static開(kāi)始的字符串,請(qǐng)問(wèn)正則該如何寫(xiě)?感謝

也就是staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png

另外由于url前半段可能會(huì)變動(dòng),所以最好還是用正則的好

問(wèn)題解答

回答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']

這個(gè)問(wèn)題的亮點(diǎn):

javascript - 正則的截取匹配問(wèn)題求助

標(biāo)簽: JavaScript
相關(guān)文章: