I am using this regex to detect URLs in a text string:
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6}|:[0-9]{3,4})\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/gi
Combined with this function to replace the detected strings with links:
function linkify(text) {
return text.replace(urlRegex, function(url) {
return "<a href=" + url + ">" + url + "</a>";
});
}
Both found here: Detect URLs in text with JavaScript
This is working for the majority of links, however links such as www.fatsoma.com/flatline-cardiff
and
tickets.partyforthepeople.org/events/3633
are detected, but link to nowhere, adding the local path in front of the detected link e.g http://127.0.0.1:8000/filelocation/tickets.partyforthepeople.org/events/3633
Is this to do with the absence of a protocol such as Https at the beginning of the link?