Note that there are some explanatory texts on larger screens.

plurals
  1. POMake links clickable except when they already are using C#
    text
    copied!<p>I found some examples that use regular expressions to detect patterns for URLs inside text paragraphs and add the HTML code to make them links. The problem I have with this approach is that, sometimes, the input paragraph contains both URLs written in plain text (which I want to convert to clickable) but also some URLs that already have markup for links. For example, consider this paragraph:</p> <pre class="lang-none prettyprint-override"><code>My favourite search engine is http://www.google.com but sometimes I also use &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt; </code></pre> <p>I only want to convert the Google link but leave the two Yahoo links as they are.</p> <p>What I am after is a C# function that uses regex to detect URLs and convert them but which ignores URLs that either have "A" markup tags surrounding them or inside an "A" tag already.</p> <p><strong>Edit</strong></p> <p>Here is what I have so far:</p> <pre><code>PostBody = "My favourite search engine is http://www.google.com but sometimes I also use &lt;a href=\"http://www.yahoo.com\"&gt;http://www.yahoo.com&lt;/a&gt;"; String pattern = @"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"; System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection matches = regex.Matches(PostBody); for (int i = 0; i &lt; matches.Count; i++) { PostBody = PostBody.Replace(matches[i].Value, String.Format("&lt;a href=\"{0}\"&gt;{1}&lt;/a&gt;", matches[i].Value, matches[i].Value)); } ltrlPostBody.Text = PostBody; </code></pre> <p>And here is what I am getting (I split it in multiple lines for clarity):</p> <pre><code>My favourite search engine is &lt;a href="http://www.google.com"&gt;http://www.google.com&lt;/a&gt; but sometimes I also use &lt;a href="&lt;a href="&lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;"&gt; &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;&lt;/a&gt;"&gt; </code></pre> <p>I only want to convert the first link (in this case) because it does not already make part of a link markup.</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