Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace URLs (with links) BUT within text that already has some links defined
    primarykey
    data
    text
    <p>I reviewed the answers to the basic question in this link: <a href="https://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links/7138764#7138764">How to replace plain URLs with links?</a> and have decided to use the code from what <a href="https://stackoverflow.com/users/725349/christian-koch">Christian Koch</a> suggested, but it only partially covers what I need to do. I am hoping someone here can help me out.</p> <p>The code Christian Koch supplied works great with one exception - When I have text that already contains links and just plain text, those links are getting a double 'a' tag, therefore causing html problems that make the text not appear right in the browser.</p> <p>For instance the code works fine for this:</p> <pre><code> www.yahoo.com is a website just like http://www.google.com </code></pre> <p>I see the yahoo and google text now appear as links and both have a link wrapper around them just as I would expect:</p> <pre><code> &lt;a href="http://www.yahoo.com"&gt;www.yahoo.com&lt;/a&gt; is a website just like &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt; </code></pre> <p>Now take this text (contains basic text and a link already defined):</p> <pre><code> www.yahoo.com is a website just like &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt; </code></pre> <p>When using the code supplied, the yahoo link is correct, but the google link now has a double tag:</p> <pre><code> &lt;a href="http://www.yahoo.com"&gt;www.yahoo.com&lt;/a&gt; is a website just like &lt;a href="&lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;" target="_blank"&gt;&lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt;&lt;/a&gt; </code></pre> <p><strong>Can someone please help me get the pattern correct so that when the text already contains a link, the pattern ignores it, but still replaces the other text without a tag. I only want the pattern to do the replacing if and only if the text is not already contained in a link tag.</strong></p> <p>Here is the code i am using from the other post:</p> <pre><code> doLinks: function(originalText) { // http://, https://, ftp:// var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&amp;@#\/%?=~_|!:,.;]*[a-z0-9-+&amp;@#\/%=~_|]/gim; // www. sans http:// or https:// var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim; // Email addresses *** here I've changed the expression *** var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim; return originalText .replace(urlPattern, '&lt;a target="_blank" href="$&amp;"&gt;$&amp;&lt;/a&gt;') .replace(pseudoUrlPattern, '$1&lt;a target="_blank" href="http://$2"&gt;$2&lt;/a&gt;') .replace(emailAddressPattern, '&lt;a target="_blank" href="mailto:$1"&gt;$1&lt;/a&gt;'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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