How to replace plain URLs with links?

前端 未结 24 2301
生来不讨喜
生来不讨喜 2020-11-21 05:42

I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing t

24条回答
  •  遥遥无期
    2020-11-21 05:45

    The e-mail detection in Travitron's answer above did not work for me, so I extended/replaced it with the following (C# code).

    // Change e-mail addresses to mailto: links.
    const RegexOptions o = RegexOptions.Multiline | RegexOptions.IgnoreCase;
    const string pat3 = @"([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,6})";
    const string rep3 = @"$1@$2.$3";
    text = Regex.Replace(text, pat3, rep3, o);
    

    This allows for e-mail addresses like "firstname.secondname@one.two.three.co.uk".

提交回复
热议问题