Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well after giving the problem another look, I've come to believe that a combination of several sub-patterns going to do a better job then one mega-pattern. So I divided the pseudoUrlPattern into two, one for urls at the beginning of the line, and one for every other url in the given text. Consider the following revised code, complete with my testing text:</p> <pre><code> var doLinks = function(originalText) { var urlPattern = /[^&lt;&gt;]\b(?:https?|ftp):\/\/[a-z0-9-+&amp;@#\/%?=~_|!:,.;]*[a-z0-9-+&amp;@#\/%=~_|](?![^&lt;&gt;])/gim; var pseudoUrlPattern1 = /^([^\/])?(www\.[\S]+(\b|$|[^&lt;&gt;]))/gim var pseudoUrlPattern2 = /([^\/"&gt;&lt;])(www\.[\S]+(\b|$))(?![^&lt;&gt;])?/gim; 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(pseudoUrlPattern1, '$1&lt;a target="_blank" href="http://$2"&gt;$2&lt;/a&gt;') .replace(pseudoUrlPattern2, '$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;'); } var string = 'www.yahoo.com is a website just like &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt; and not like &lt;a href="www.facebook.com "&gt; www.facebook.com &lt;/a&gt; and not like www.example.com'; </code></pre> <p>give it a try, tell me how it went.</p> <p>one thing to pay attention to: the urls already in the anchor tags preferably should not have any spaces between the url and the tag.</p>
    singulars
    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.
    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