Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First you need a good regex that matches urls. This is hard to do. See <a href="https://stackoverflow.com/questions/1410311/regular-expression-for-url-validation-in-javascript/1411800#1411800">here</a>, <a href="https://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python/827621#827621">here</a> and <a href="https://stackoverflow.com/questions/226505/question-about-url-validation-with-regex/226709#226709">here</a>:</p> <blockquote> <p>...almost anything is a valid URL. There are some punctuation rules for splitting it up. Absent any punctuation, you still have a valid URL.</p> <p>Check the RFC carefully and see if you can construct an "invalid" URL. The rules are very flexible. </p> <p>For example <code>:::::</code> is a valid URL. The path is <code>":::::"</code>. A pretty stupid filename, but a valid filename.</p> <p>Also, <code>/////</code> is a valid URL. The netloc ("hostname") is <code>""</code>. The path is <code>"///"</code>. Again, stupid. Also valid. This URL normalizes to <code>"///"</code> which is the equivalent.</p> <p>Something like <code>"bad://///worse/////"</code> is perfectly valid. Dumb but valid.</p> </blockquote> <p>Anyway, this answer is not meant to give you the best regex but rather a proof of how to do the string wrapping inside the text, with JavaScript.</p> <p>OK so lets just use this one: <code>/(https?:\/\/[^\s]+)/g</code></p> <p>Again, <em>this is a bad regex</em>. It will have many false positives. However it's good enough for this example.</p> <pre><code>function urlify(text) { var urlRegex = /(https?:\/\/[^\s]+)/g; return text.replace(urlRegex, function(url) { return '&lt;a href="' + url + '"&gt;' + url + '&lt;/a&gt;'; }) // or alternatively // return text.replace(urlRegex, '&lt;a href="$1"&gt;$1&lt;/a&gt;') } var text = "Find me at http://www.example.com and also at http://stackoverflow.com"; var html = urlify(text); // html now looks like: // "Find me at &lt;a href="http://www.example.com"&gt;http://www.example.com&lt;/a&gt; and also at &lt;a href="http://stackoverflow.com"&gt;http://stackoverflow.com&lt;/a&gt;" </code></pre> <p>So in sum try:</p> <pre><code>$$('#pad dl dd').each(function(element) { element.innerHTML = urlify(element.innerHTML); }); </code></pre>
 

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