题解 | #替换链接#

替换链接

https://www.nowcoder.com/practice/4578a70b955848ff8e95c1f88c94c1ba

function link() {
    let str = jsContainer.innerText;
    //let str = 'test http://www.testhaha.com test  test ';
    const w = '[a-zA-Z0-9]+'
    const reg = new RegExp(`((https://)|(http://))*w{3}\\.${w}(\\.(com|cn))+(\\?${w}=\\w*(#\\w*)*)*`,'g');
    const res = str.match(reg);

    if(!!res) {
        let attr = '';
        res.forEach( s => {
            str = str.replace(s,function(str) {
                attr = s.indexOf('www')== 0 ? 'http://' + s : s;
                return `<a href=${attr} target="_blank">${str.trim()}</a>`;
            })
        })
        jsContainer.innerHTML = str;
    }

}

link();

全部评论

相关推荐

面了100年面试不知...:今年白菜这么多,冬天可以狂吃了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务