用正則表達(dá)式替換圖片地址img標(biāo)簽
開始想到的解決方法是:
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match) {
console.log(match);
});
輸出結(jié)果是:
<img src="http://www.dbjr.com.cn/images/logo.gif" alt="" width="142" height="55" />
得到的是整個(gè)img標(biāo)簽,但我期望得到的是src中的網(wǎng)址,這樣只需在function(match)中返回新地址就行了。
于是,卡在這里了。。。
后來(lái),通過(guò)Google搜索關(guān)鍵字“javascript replace callback”,在stackoverflow中找到了“replace callback function with matches”,才知道function(match)還有其他參數(shù)
然后,改為下面的代碼,問(wèn)題就解決了。
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
console.log(capture);
});
輸出結(jié)果:
http://www.dbjr.com.cn/images/logo.gif
相關(guān)文章
document.styleSheets[0].disabled
document.styleSheets[0].disabled...2006-10-10javascript函數(shù)中的arguments參數(shù)
arguments當(dāng)然只在function體內(nèi)才有意義, arguments.length 返回的是傳入function的實(shí)參個(gè)數(shù)2010-08-08window.onload和$(function(){})的區(qū)別介紹
window.onload和$(function(){})有什么區(qū)別。window.onload表示頁(yè)面加載完了后(包括dom和js),再執(zhí)行函數(shù)里面的內(nèi)容,感興趣的朋友可以了解下2013-10-10