js html text,Javascript truncate HTML text

本文介绍了一种JavaScript函数,用于处理HTML内容过长时的截断问题,包括去除多余标签、修复不完整的结束标签,并确保所有标签正确闭合。通过逆向解析和自动添加缺失的结束标签,提高了HTML片段的完整性和可读性。
摘要由CSDN通过智能技术生成

I had the same problem, and wound up writing the following to deal with it. It truncates HTML to a give length, cleans up any start / end tags that might have gotten snipped off at the end, and then closes any tags left unclosed:

function truncateHTML(text, length) {

var truncated = text.substring(0, length);

// Remove line breaks and surrounding whitespace

truncated = truncated.replace(/(\r\n|\n|\r)/gm,"").trim();

// If the text ends with an incomplete start tag, trim it off

truncated = truncated.replace(/

// If the text ends with a truncated end tag, fix it.

var truncatedEndTagExpr = /

var truncatedEndTagMatch = truncatedEndTagExpr.exec(truncated);

if (truncatedEndTagMatch != null) {

var truncatedEndTag = truncatedEndTagMatch[1];

// Check to see if there's an identifiable tag in the end tag

if (truncatedEndTag.length > 0) {

// If so, find the start tag, and close it

var startTagExpr = new RegExp(

"");

var testString = truncated;

var startTagMatch = startTagExpr.exec(testString);

var startTag = null;

while (startTagMatch != null) {

startTag = startTagMatch[1];

testString = testString.replace(startTagExpr, '');

startTagMatch = startTagExpr.exec(testString);

}

if (startTag != null) {

truncated = truncated.replace(truncatedEndTagExpr, '' + startTag + '>');

}

} else {

// Otherwise, cull off the broken end tag

truncated = truncated.replace(truncatedEndTagExpr, '');

}

}

// Now the tricky part. Reverse the text, and look for opening tags. For each opening tag,

// check to see that he closing tag before it is for that tag. If not, append a closing tag.

var testString = reverseHtml(truncated);

var reverseTagOpenExpr = //;

var tagMatch = reverseTagOpenExpr.exec(testString);

while (tagMatch != null) {

var tag = tagMatch[0];

var tagName = tagMatch[2];

var startPos = tagMatch.index;

var endPos = startPos + tag.length;

var fragment = testString.substring(0, endPos);

// Test to see if an end tag is found in the fragment. If not, append one to the end

// of the truncated HTML, thus closing the last unclosed tag

if (!new RegExp("").test(fragment)) {

truncated += '' + reverseHtml(tagName) + '>';

}

// Get rid of the already tested fragment

testString = testString.replace(fragment, '');

// Get another tag to test

tagMatch = reverseTagOpenExpr.exec(testString);

}

return truncated;

}

function reverseHtml(str) {

var ph = String.fromCharCode(206);

var result = str.split('').reverse().join('');

while (result.indexOf(' -1) {

result = result.replace('

}

while (result.indexOf('>') > -1) {

result = result.replace('>', '

}

while (result.indexOf(ph) > -1) {

result = result.replace(ph, '>');

}

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值