Forum Moderators: open
a = a.replace(
/(^|>|\s)(https?:\/\/)?(www\.\S+)/gi,
'$1<a href="https://$3">$3</a>'); /(^|>|\s)Do you need to constrain it to those specific characters? Otherwise you could simply say \b and sidestep the capture. Instead you can capture the (https?)--which is already right there, only it can be $1 instead of $2--and reuse it.
Do you need to constrain it to those specific characters?
If you can afford the extra lines, there's always "if $2 == ''" and so on.
a = a.replace(
/(^|>|\s)(https?:\/\/)?(www\.\S+)/gi,
($match, $1, $2, $3) => {
return $1 + '<a href="' + ($2 || 'http://') + $3 + '">' + $3 + '</a>';
}
);