I'm trying to convert text containing URL's into HTML anchors using Visual Basic in ASP.NET 2.0, so far I have this (which is working) but it only picks up http and https:
Regex.Replace(message, "https?://[^\s]*", "<a href=""$0"">$0</a>", RegexOptions.IgnoreCase)
I would like it to be able to also pick up anything starting "www.", so I tried the following:
Regex.Replace(message, "(https?://|www\.)[^\s]*", "<a href=""$0"">$0</a>", RegexOptions.IgnoreCase)
However, if it starts with "www." the first $0 in the replacement requires an additional "http://" to be put in front... and I haven't got a clue how to do it (or if it's possible).
尝试这个:
Regex.Replace(message, "(?:http(s?)://|(www\.))([^\s]+)", "<a href=""http$1://$2$3"">http$1://$2$3</a>", RegexOptions.IgnoreCase)
Convert text containing URL's into HTML anchors and insert http:// or https:// if it isn't in the input string :
string input = "text www.stackoverflow.com/questions message";
string pattern = @"(http(?<ssl>s?)://|(?<www>www\.))(?<url>[^\s]*)";
string replacement = "<a href=\"http${ssl}://${www}${url}\">http${ssl}://${www}${url}</a>";
Console.WriteLine(Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.