查找/选择html文本中的前250个字符

本文关键字:250个 字符 文本 选择 html 查找 | 更新日期: 2023-09-27 18:29:34

例如:

<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

Regex:

<[^<>]+>[^<>.!?]{250}

它应该选择忽略html标记的前250个字符。它忽略第一个html标记的出现,但选择第二个

查找/选择html文本中的前250个字符

当然还有更优雅的东西,但由于你没有那么努力地格式化你的问题,我会尝试一些不同的东西,只是为了它。尽管如此,它还是有效的。

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;
}

正则表达式按预期工作,您没有将其锚定在字符串的开头,因此当它在第一个<p>标记上不匹配时,它会在字符串中查找匹配项。它在第二个标签<span class="A1">上也不匹配,所以它继续,直到找到可以匹配的地方。

这个地方原来是第三个HTML标记,即<span style="font-size: 10pt;">标记,它是第一个后面跟着250个非<>.!?字符的标记,因此它与该标记和后面的250个字符相匹配。

所以,一切如预期。

我使用find,因为控件在用户控件页中。否则类名就足够

$("#id").find(".class").each(function (i) {
    var len = $(this).text().length;
    if (len > 250) {
        $(this).text($(this).text().substr(0, 250) + '...');
    }
});