Long story short, I have a website made under Wix.com editor, and coding was made possible a few months ago.
I have set up a custom comment box, so users can post their comments, and read others'.
Now the thing is, the "comment Input" takes plain text, and whenever a link is posted, it is displayed as plain text, no color, no clickability.
I want a code that 'reads' the list of comments, and convert every text that begins with 'https' or 'http' or 'www' ... orange and clickable (opening in a new tab)
Any solution please ?
Thanks !
I have tried many things such as :
$w('#text95').html =
(/((http:|https:)[^\s]+[\w])/g, '<a href="$1" target="_blank">$1</a>').replace;
text95 = the displayed comments (it is a text that repeats itself for as many comments as there are)
It looks like you're replace syntax is wrong.
Try something like this, I'm pretty sure this will work.
function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links.
replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText;
}
Calling it with:
$w('#text95').innerHTML = linkify($w('#text95').html);
I'm not sure what $w
is or if you can really assign the html like that, but i'm guessing this is jquery since the $
most commonly refers to the jquery object.
Your try was close, it would be..
$('#text95').html($('#text95').html().replace(/((http:|https:)[^\s]+[\w])/g, '<a href="$1" target="_blank">$1</a>'));
try it..
$('#text95').html($('#text95').html().replace(/((http:|https:)[^\s]+[\w])/g, '<a href="$1" target="_blank">$1</a>'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=text95>
stuff and stuff and http://ww.stuff.com stuff
</div>
here is my answer:
function convertLinks(text) {
var link = text.match(/(?:www|https?)[^\s]+/g),
aLink = [],
replacedText = text;
if (link != null) {
for (i=0;i<link.length;i++) {
var replace;
if (!( link[i].match(/(http(s?))\:\/\//) ) ) { replace = 'http://' + link[i]; } else { replace = link[i] };
var linkText = replace.split('/')[2];
if (linkText.substring(0,3) == "www") { linkText = linkText.replace('www.','') };
aLink.push('<a href="' + replace + '" target="_blank">' + linkText + '</a>');
replacedText = replacedText.split(link[i]).join(aLink[i]);
}
return repText
} else {
return text
}
}
this replaces long and clumsy links within plain texts to short clickable links within that text.
Example:
This clumsy link https://stackoverflow.com/questions/49634850/javascript-convert-plain-text-links-to-clickable-links/52544985#52544985 is very clumsy and this http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split is not much better. This one www.apple.com is nice but www could be omitted
Becomes:
This clumsy link <a href="https://stackoverflow.com/questions/49634850/javascript-convert-plain-text-links-to-clickable-links/52544985#52544985" target="_blank">stackoverflow.com</a> is very clumsy and this <a href="http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split" target="_blank">developer.mozilla.org</a> is not much better. This one <a href="http://www.apple.com" target="_blank">apple.com</a> is nice but www could be omitted
Is displayed as:
This clumsy link stackoverflow.com is very clumsy and this developer.mozilla.org is not much better. This one apple.com is nice but www can be removed