I'm developing a community site. When a user posts a comment that contains a URL, I want to enclose that url in an <a>
tag.
Input
Hello. Please take a look at this video.
https://example.com
Output
Hello. Please take a look at this video.
<a href="https://example.com">https://example.com</a>
Code
I would like to know what I should put in regex
and sanitizer
below.
const el = document.getElementById('comment')
const commentText = `Hello. Please take a look at this video.\nhttps://example.com`
el.innerHTML = commentText.replace(/regex/g, function(match){
const sanitizedText = sanitizer(match)
return `<a href="${sanitizedText}">${sanitizedText}</a>`
})
Or if there is a better way, please let me know.