从字符串的开头和结尾删除BR标签

Ric*_*ves 2 c# regex string

我如何使用类似

return Regex.Replace("/(^)?(<br\s*\/?>\s*)+$/", "", source);
Run Code Online (Sandbox Code Playgroud)

替换这种情况:

<br>thestringIwant => thestringIwant
<br><br>thestringIwant => thestringIwant
<br>thestringIwant<br> => thestringIwant
<br><br>thestringIwant<br><br> => thestringIwant
thestringIwant<br><br> => thestringIwant
Run Code Online (Sandbox Code Playgroud)

它可以在开头或结尾有多个br标签,但是我不想在中间删除任何br标签。

Ale*_*lex 5

几个循环可以解决该问题,并且更易于阅读和理解(使用正则表达式=明天您将查看自己的代码,以了解到底发生了什么)

while(source.StartsWith("<br>")) 
    source = source.SubString(4);
while(source.EndsWith("<br>"))  
    source = source.SubString(0,source.Length - 4);

return source;
Run Code Online (Sandbox Code Playgroud)