js實現截取a標簽的href屬性和內容


    showCatalog = () => {//食堂百科-docx頁面添加目錄
        let docxStr = document.getElementById("docx").innerHTML,
            reg = /<a href="(.+?)">(.+?)<\/a>/gi,
            regCont = /<a[^>]*href=['"]([^"]*)['"][^>]*>(.*?)<\/a>/gi,
            nodeArr = docxStr.match(reg),//a標簽的數組
            hrefArr = [],//截取出來的href屬性
            codeArr = [],//截取的a標簽內容
            catalogAryList = [],//目錄總數據
            hrefReg = /(<a\b[^>]+\bhref=")(\S*)("[^>]*>)(.*?)(<\/a>)/;
        if (nodeArr !== null && nodeArr !== [] && nodeArr !== undefined) {
            for (let key in nodeArr) {
                let str = nodeArr[key];
                if (str.indexOf("#") >= 0) {
                    hrefArr.push((hrefReg.exec(str))[2])
                    codeArr.push((hrefReg.exec(str))[4])
                }
            }
            hrefArr.map((item, index) => {
                catalogAryList.push({
                    id: index,
                    href: item,//鏈接
                    msg: codeArr[index]//內容
                })
            })
            catalogObj.catalogAry = catalogAryList;
        } else {
           // console.log('empty')
        }

    }

 


免責聲明!

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



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