例如:
<p class="Pa0" style="margin: 0in 0in 0pt;"><span class="A1"><span style="font-size: 10pt;">Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers Giving this flyer to your class customers</span></span></p>Giving this flyer to your class customers Giving this flyer to your class customers
正则表达式:
<[^<>]+>[^<>.!?]{250}
它应该选择忽略html标签的所有前250个字符。它忽略了第一个html标签出现,但选择了第二个
答案 0 :(得分:1)
当然有更优雅的东西,但是你没有那么努力地格式化你的问题,我会尝试不同的东西,只是为了它。尽管如此,它还是有效的。
var str = @"<p>something here<H3>Title</H3></p>";
// Add and remove "<" chars on the stack. When we don't have any "<"
// chars on the stack, it means we're in the contents sections of the tag.
var stack = new Stack<string>();
// Avoid peeking an empty stack.
stack.Push("base");
// This will be your resulting string and number of chars.
var result = "";
var resultLimit = 5;
foreach (var ch in str)
{
// Limit reached.
if (result.Count() == resultLimit)
break;
// Entering a tag.
if (ch == '<')
{ stack.Push("start"); continue; }
// Leaving a tag.
if (ch == '>')
{ stack.Pop(); continue; }
// We're not in a tag at the moment, so take this char.
if (stack.Peek() != "start")
result += ch;
}
答案 1 :(得分:1)
正则表达式按预期工作,你没有将它锚定到字符串的开头,所以当它在第一个<p>
标记上无法匹配时,它会继续通过你的字符串寻找匹配。它也无法与第二个标记<span class="A1">
匹配,因此它会一直持续到找到可以匹配的位置。
该地方原来是第三个HTML标记,<span style="font-size: 10pt;">
标记是第一个标记,后跟250个非<>.!?
个字符,因此它与该标记和后续250个字符匹配。
所以,所有工作都按预期进行。
答案 2 :(得分:0)
我使用了find,因为控件在用户控制页面中。其他类名就足够了
$("#id").find(".class").each(function (i) {
var len = $(this).text().length;
if (len > 250) {
$(this).text($(this).text().substr(0, 250) + '...');
}
});