js用正則表達式替換字符串中的圖片地址(img src),期望取到src值,出來的卻是完整的標簽問題


開發中遇到需要匹配htmk字符串中包含的所有img src的值,要用正則表達式去匹配出來,用在線正則測試工具測試,/<img [^>]*src=['"]([^'"]+)[^>]*>/,可以匹配到html中的src值,但是用在js上輸出的結果卻是:
<img src="http://static.cnblogs.com/images/logo_small.gif" alt="" width="142" height="55" />
將整個html的標簽都給返回了回來,這樣很麻煩,與期望的 http://static.cnblogs.com/images/logo_small.gif
並不一致。
看到一篇博客,講到
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match) {
console.log(match);
});
function中並不止有match一個參數,還有其他參數,capture
content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
console.log(capture);
});
capture輸出就是我想要的結果
參考博客來源者:博客園@dudu


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM