最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Asp.net C# Replacing all the urls of a string with an "a href" link - Stack Overflow

programmeradmin2浏览0评论

I am currently displaying the content of a string inside a pre tag, but I have to elaborate a function that for each link into the string replace it with a link tag, I tried several string replace and regex methods, but no one worked.

string myString = "Bla bla bla bla  and bla bla  blabla"
//Logic
string outputString = "Bla bla bla bla <a href="" target="blank">;/a> and bla bla <a href="" target="blank">;/a> blabla"

I have used the following code, but it does not work for every url:

string orderedString = item.Details.Replace("|", "\n" );
string orderedStringWithUrl = "";

System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.MatchCollection mactches = regx.Matches(orderedString);

foreach (System.Text.RegularExpressions.Match match in mactches)
{
    orderedStringWithUrl = orderedString.Replace(match.Value, "<a href='" + match.Value + "' target='blank'>" + match.Value + "</a>");
}

Any suggestion?

Update: I have noticed that the URLs I have in the string are all without spaces and all starts with http or https. Is it an idea the one to put into a everything that starts with http or https up to (and not included) the first space? In that case how can I use the .replace to achieve this?

Tnx in advance.

I am currently displaying the content of a string inside a pre tag, but I have to elaborate a function that for each link into the string replace it with a link tag, I tried several string replace and regex methods, but no one worked.

string myString = "Bla bla bla bla http://www.site. and bla bla http://site2. blabla"
//Logic
string outputString = "Bla bla bla bla <a href="http://www.site." target="blank">http://www.site.</a> and bla bla <a href="http://site2." target="blank">http://site2.</a> blabla"

I have used the following code, but it does not work for every url:

string orderedString = item.Details.Replace("|", "\n" );
string orderedStringWithUrl = "";

System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

System.Text.RegularExpressions.MatchCollection mactches = regx.Matches(orderedString);

foreach (System.Text.RegularExpressions.Match match in mactches)
{
    orderedStringWithUrl = orderedString.Replace(match.Value, "<a href='" + match.Value + "' target='blank'>" + match.Value + "</a>");
}

Any suggestion?

Update: I have noticed that the URLs I have in the string are all without spaces and all starts with http or https. Is it an idea the one to put into a everything that starts with http or https up to (and not included) the first space? In that case how can I use the .replace to achieve this?

Tnx in advance.

Share Improve this question edited Aug 15, 2011 at 10:56 Attila asked Aug 15, 2011 at 7:08 AttilaAttila 7121 gold badge13 silver badges34 bronze badges 4
  • A sample would help. Probably you need to use a literal control to display the string to have it interpreted as HTML – Sascha Commented Aug 15, 2011 at 7:14
  • 1 very closely related: stackoverflow./questions/758135/… – codeandcloud Commented Aug 15, 2011 at 7:17
  • I'm not sure I entirely understand the question; so you have a string in a tag somewhere on the page that contains a list of Urls that need to be converted with javascript? It would be very helpful if you gave a simple example of what you mean. – shelleybutterfly Commented Aug 15, 2011 at 7:22
  • I modified the answer :) – Attila Commented Aug 15, 2011 at 9:16
Add a ment  | 

2 Answers 2

Reset to default 3

In a sample I used this markup

<body>
    <form id="form1" runat="server">
    <div>
      <asp:Literal ID="litTextWithLinks" runat="server" />
    </div>
    </form>
</body>

along with that code-behind

private const string INPUT_STRING = "Bla bla bla bla http://www.site. and bla bla http://site2. blabla";

protected void Page_Load ( object sender, EventArgs e ) {
  var outputString = INPUT_STRING;

  Regex regx = new Regex( @"https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?", RegexOptions.IgnoreCase );

  MatchCollection mactches = regx.Matches( INPUT_STRING );
  foreach ( Match match in mactches ) {
    outputString = outputString.Replace( match.Value, String.Format( "<a href=\"{0}\" target=\"_blank\">{0}</a>", match.Value ) );
  }

  litTextWithLinks.Text = outputString;
}

For all URLs from your sample where correctly replaced with links that opened in a new browser window.

You could test the url by doing a WebRequest and only replacing if opening succeeds. If not all URLs match, then you probably need to change the regular expression.

If this doesn't answer your question, you should add some more detail.

check this out: http://alexlittle/blog/2009/06/26/regex-for-replacing-url-with-anchor-tag/

发布评论

评论列表(0)

  1. 暂无评论