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