Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since <a href="https://stackoverflow.com/a/1277583/1503784">javascript doesn't seem to support negative look-behind</a>, you will have to trick it by using a replace function. Capture the <code>href</code> (maybe you should also also consider <code>src</code>) :</p> <pre><code>function repl(text) { var exp = /((href|src)=["']|)(\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/ig; return text.replace(exp, function() { return arguments[1] ? arguments[0] : "&lt;a href=\"" + arguments[3] + "\"&gt;" + arguments[3] + "&lt;/a&gt;" }); } </code></pre> <p>See the <a href="http://jsfiddle.net/VGVhv/" rel="nofollow noreferrer">demo</a></p> <p><strong>EDIT</strong></p> <p>A "better" version which will only replace links in actual text nodes:</p> <pre><code>function repl(node) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/i; var nodes=node.childNodes; for (var i=0, m=nodes.length; i&lt;m; i++){ var n=nodes[i]; if (n.nodeType==n.TEXT_NODE) { var g=n.textContent.match(exp); while(g) { var idx=n.textContent.indexOf(g[0]); var pre=n.textContent.substring(0,idx); var t=document.createTextNode(pre); var a=document.createElement("a"); a.href=g[0]; a.innerText=g[0]; n.textContent = n.textContent.substring(idx+g[0].length); n.parentElement.insertBefore(t,n); n.parentElement.insertBefore(a,n); g=n.textContent.match(exp); } } else { repl(n); } } } var r=repl(document.getElementById("t")) </code></pre> <p>​ See the <a href="http://jsfiddle.net/mAJkL/" rel="nofollow noreferrer">demo</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload