I have some links in which people did not add the protocol to. I.e., http://www.stackoverflow.com. If the link begins with http://www., I want to replace it with ‘http://www.’.
How can I do this with JavaScript regular expressions?
I tried the code below, but I can’t seem to match the pattern ‘doesn’t start with [A-z]+://www.’.
The links are mixed in with text.
jQuery(document).ready(function () {
jQuery('.myClass').each(function (index) {
var temp = wwwify(jQuery(this).text());
jQuery(this).html(temp);
});
});
function wwwify(text) {
var regex = /(?!\b([A-z]+:\/\/))www\./igm;
return text.replace(regex, 'http://www.');
}
Since I haven’t found any suitable regex solutions through SO or elsewhere, just using regular javascript replace may be the best solution.
For now I’m making two passes through the text: