什么是去除的最佳方式< BR>标签从字符串的结尾?字符串、结尾、标签、方式

2023-09-04 00:57:19 作者:除了美我一无所有

在.NET Web系统我的工作使最终用户在某些情况下,输入HTML格式的文本。在其中一些地方,我们希望把所有的标签,但去掉所有尾部断裂标签(但留下任何符文的体内。)

The .NET web system I'm working on allows the end user to input HTML formatted text in some situations. In some of those places, we want to leave all the tags, but strip off any trailing break tags (but leave any breaks inside the body of the text.)

什么是做到这一点的最好方法是什么? (我能想到的方法可以做到这一点,但我敢肯定,他们不是最好的。)

What's the best way to do this? (I can think of ways to do this, but I'm sure they're not the best.)

推荐答案

作为@ 米奇说,

//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Thu, Sep 25, 2008, 02:01:36 PM
///  Using Expresso Version: 2.1.2150, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  Match expression but don't capture it. [\<br\s*/?\>], any number of repetitions
///      \<br\s*/?\>
///          <
///          br
///          Whitespace, any number of repetitions
///          /, zero or one repetitions
///          >
///  End of line or string
///  
///  
/// </summary>
public static Regex regex = new Regex(
    @"(?:\<br\s*/?\>)*$",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
regex.Replace(text, string.Empty);