【问题标题】:Substring text with HTML tags in JavascriptJavascript中带有HTML标签的子字符串文本
【发布时间】:2011-08-25 14:04:50
【问题描述】:

您有解决方案在 Javascript 中使用 HTML 标记对文本进行子字符串处理吗?

例如:

var str = 'Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, consectetur adipiscing elit.'

html_substr(str, 20)
// return Lorem ipsum <a href="#">dolor <strong>si</strong></a>

html_substr(str, 30)
// return Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, co

【问题讨论】:

  • 您似乎希望子字符串忽略标签,但在最终结果中保持它们完好无损。我认为您需要将字符串转换为 DOM 元素,遍历元素,计算文本节点中的字符,并删除所有超出计数的字符(或文本节点)。即使这样,我也有一种感觉,浏览器之间在空白方面可能存在一些差异。不过不确定。
  • 发布了答案。似乎给出了您想要的结果,但是浏览器之间在空白方面可能存在一些差异。不确定。
  • 子字符串 html 代码没有像 [this][1] 那样破坏 html。 [1]:stackoverflow.com/questions/6118904/…

标签: javascript html tags substring


【解决方案1】:

Javascript 有一个子字符串方法。如果字符串包含 html,则没有区别。

http://www.w3schools.com/jsref/jsref_substr.asp

【讨论】:

  • 是的,我知道。但我的问题是,当我使用 substr 时,html 标签可能会被破坏。
  • 在这种情况下,您可以查看诸如递归正则表达式之类的东西来平衡html标签。但这将非常复杂。
【解决方案2】:

使用类似于= str.replace(/&lt;[^&gt;]*&gt;?/gi, '').substr(0, 20);的内容
我在以下位置创建了一个示例:http://fiddle.jshell.net/xpW9j/1/

【讨论】:

  • 这不符合 OP 的要求。在示例结果中,保留了标签。
【解决方案3】:

用法:

var str = 'Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, consectetur adipiscing elit.';

var res1 = html_substr( str, 20 );
var res2 = html_substr( str, 30 );

alert( res1 ); // Lorem ipsum <a href="#">dolor <strong>si</strong></a>
alert( res2 ); // Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, co

示例: http://jsfiddle.net/2ULbK/4/


功能:

function html_substr( str, count ) {

    var div = document.createElement('div');
    div.innerHTML = str;

    walk( div, track );

    function track( el ) {
        if( count > 0 ) {
            var len = el.data.length;
            count -= len;
            if( count <= 0 ) {
                el.data = el.substringData( 0, el.data.length + count );
            }
        } else {
            el.data = '';
        }
    }

    function walk( el, fn ) {
        var node = el.firstChild;
        do {
            if( node.nodeType === 3 ) {
                fn(node);
                    //          Added this >>------------------------------------<<
            } else if( node.nodeType === 1 && node.childNodes && node.childNodes[0] ) {
                walk( node, fn );
            }
        } while( node = node.nextSibling );
    }
    return div.innerHTML;
}

【讨论】:

  • 我认为仅仅返回div.innerHTML 是不够的。考虑如果在切割点之后有更多标签会发生什么。它们最终会出现在最终的字符串中,但为空...我认为一旦count&lt;=0 你应该删除剩余的元素,而不是设置data = ''
  • @Dan:是的,这是真的。我不确定哪个OP想要。可能是潜在的空标签应该作为 DOM 结构的一部分留在原处。但你是对的,如果不是这种情况,那么你会改用el.parentNode.removeChild(el)。编辑:实际上这会搞乱 DOM 的运行。
  • @patrick dw:这是一个删除剩余节点的updated jsFiddle
  • 谢谢哥们。这个解决方案很棒。但是非对标签(img,hr,...)存在一些问题。效果很好!
  • @honzahommer:你能举一个给你带来麻烦的 HTML 字符串的例子吗?此外,您想对完全清空的标签(全部内容超过计数的标签)做什么?这些标签应该被删除,还是保留为空标签?
【解决方案4】:

考虑到parsing html with regex is a bad idea,这里有一个解决方案:)

编辑:要明确一点:这不是一个有效的解决方案,它的目的是对输入字符串做出非常宽松的假设,因此应该谨慎对待。阅读上面的链接,看看为什么永远无法使用正则表达式解析 html。

function htmlSubstring(s, n) {
    var m, r = /<([^>\s]*)[^>]*>/g,
        stack = [],
        lasti = 0,
        result = '';

    //for each tag, while we don't have enough characters
    while ((m = r.exec(s)) && n) {
        //get the text substring between the last tag and this one
        var temp = s.substring(lasti, m.index).substr(0, n);
        //append to the result and count the number of characters added
        result += temp;
        n -= temp.length;
        lasti = r.lastIndex;

        if (n) {
            result += m[0];
            if (m[1].indexOf('/') === 0) {
                //if this is a closing tag, than pop the stack (does not account for bad html)
                stack.pop();
            } else if (m[1].lastIndexOf('/') !== m[1].length - 1) {
                //if this is not a self closing tag than push it in the stack
                stack.push(m[1]);
            }
        }
    }

    //add the remainder of the string, if needed (there are no more tags in here)
    result += s.substr(lasti, n);

    //fix the unclosed tags
    while (stack.length) {
        result += '</' + stack.pop() + '>';
    }

    return result;

}

示例: http://jsfiddle.net/danmana/5mNNU/

注意:patrick dw's solution 对于坏 html 可能更安全,但我不确定它处理空格的效果如何。

【讨论】:

  • &lt;img src='blah' title='Yes/No' alt='&gt;&gt;' /&gt; 不要使用正则表达式解析 html - 对于您拥有的每个正则表达式,都可以找到 html 来破坏它。
  • @Zirak:我知道 :) 你真的读过我发布的第一句话中的第一个链接吗? :) 还阅读了我的最后一句话:P 我知道这不是正确的解决方案,但我认为这对我来说是一个有趣的练习,如果我还是这样做了,为什么不发布它。
  • 所以你知道这很糟糕,但你建议它?我的示例不是无效或错误的 html。这是完全有效的。对验证器运行它,它不会发出任何声音。无效的是您的正则表达式,因为它无法匹配所有有效的 html。
  • @Zirak:我从来没有说过这是一个有效的解决方案,当然正则表达式是无效的,它从来都不是。这只是一个对输入字符串做出一些疯狂假设的练习......我将编辑帖子并使其更清晰
  • 如何从上面的函数中得到剩余的字符串
【解决方案5】:

单标签解决方案

function subStrWithoutBreakingTags(str, start, length) {
    var countTags = 0;
    var returnString = "";
    var writeLetters = 0;
    while (!((writeLetters >= length) && (countTags == 0))) {
        var letter = str.charAt(start + writeLetters);
        if (letter == "<") {
            countTags++;
        }
        if (letter == ">") {
            countTags--;
        }
        returnString += letter;
        writeLetters++;
    }
    return returnString;
}

【讨论】:

    【解决方案6】:
    let str = 'Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, consectetur adipiscing elit.'
    let plainText = htmlString.replace(/<[^>]+>/g, '');
    

    使用上面给定的正则表达式提取纯文本,然后使用基于 JS 字符串的“.substr()”函数获得所需的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 2022-11-22
      • 2019-11-18
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多