I am really new to Regex. almost know nothing. but I know js a bit. I need to detect url in my website. This regx is great one i found.
/((http|ftp|https):\/\/)?(([\w.-]*)\.([\w]*))/g
But the issue is it only select part of url. I want to edit it so it select the whole link. check this link: https://regex101.com/r/B5hYwv/2
// [EDITOR MADE THESE CHANGES]
//this code makes all urls clickable
const re = /((http|ftp|https)\:\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?/g;
let text = document.getElementById("links").textContent;
const matches = text.match(re)
console.log(matches)
for (let i = matches.length - 1; i > 0; i--) {
text = text.replace(matches[i], `<a href='${matches[i]}'>${matches[i]}</a>`);
}
document.getElementById("links").innerHTML = text;
<div id="links">
Hello www.google.com World http://yahoo.com Here are some links https://www.google.com.tr/admin/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services https://google.com.tr/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services I want them to be converted to anchor tags
http://google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services ftp://google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services www.google.com.tr/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services www.google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
drive.google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
</div>