Replace urls in string with a condition
<p>I'm trying to convert urls in a string to a link with a condition that the url must be same as the domain I'm using. How do I do this?</p>

<p>My domain is <a href="http://www.domain.com" rel="nofollow">http://www.domain.com</a></p>

<pre><code>str = "This is a www.domain.com text with url. This is another url: www.domain2.com. This is a part of a url: http://domain.com/foo.php. This is another link: http://stackoverflow.com/questions/ask. Another link: domain.com/bar.php";
</code></pre>

<p>What I want is:</p>

<ol>
<li>www.domain.com to be changed to <code>&lt;a href='www.domain.com'&gt;domain.com&lt;/a&gt;</code></li>
<li>www.domain2.com to stay unchanged.</li>
<li><a href="http://domain.com/foo.php" rel="nofollow">http://domain.com/foo.php</a> to be changed to <code>&lt;a href='http://domain.com/foo.php'&gt;/foo.php&lt;/a&gt;</code>.</li>
<li><a href="http://stackoverflow.com/questions/ask">http://stackoverflow.com/questions/ask</a> to stay unchanged.</li>
<li>domain.com/bar.php to be changed to <code>&lt;a href='domain.com/bar.php'&gt;/bar.php&lt;/a&gt;</code></li>
</ol>

<p>How can I do this with javascript?</p>
