题解 | #替换链接#
替换链接
http://www.nowcoder.com/practice/4578a70b955848ff8e95c1f88c94c1ba
function link() {
const content = document.querySelector('#jsContainer')
const html = content.innerHTML
const newHtml = html.replace(/((http|https):\/\/)?www\.\w*\.com(\.cn)?(\?\w*=\w*)?(&(\w*=\w*))*/g, function(res) {
if(res.startsWith('www')) {
const url = res.replace('www', 'http://www')
return `<a href="${url}" target="_blank">${res}</a>`
} else {
return `<a href="${res}" target="_blank">${res}</a>`
}
})
content.innerHTML = newHtml
}
查看22道真题和解析