检查一段文本中的HTML标签是否闭合,若不闭合则修复

    在论坛上看到过几次这样的问题,就写了点放到这里吧 :) 

    Java的正则引擎(java.util.regex)做不到未知层次递归匹配,再说这个用正则效率不会高,所以考虑其他方法实现。对于.Net平台,可尝试一下这个正则:^[^<>]*(?:<(?!/)(?<o>[^/s<>]+)(?:/s[^<>]*)?(?:(?<-o>/>)|>)[^<>]*|(?<-o><//k<o>>[^<>]*))*(?(o)(?!))$ (若闭合返回true,否则返回false。不会忽略单双引号之间的内容,标签也不可以交叉)

    如果要求严格可用xml方式验证,如<div id="test"name="test2"></div>(属性间没空格)等是不能通过的。如果不需要这么严格则可试一下下面的方法(忽略单双引号之间的内容,标签可以交叉):

若用ArrayList存放提取出来的标签效率不佳,就自己写一个TagsList,如下:

/**
 * 存放提取出来的标签
 * 
@author Liw
 * @time 2007-6
 
*/


import  java.util.Arrays;

class  TagsList 
{
    
private String[] data;
    
private int size = 0;

    
public TagsList(int size) 
    
{
        data 
= new String[size];
    }


    
public TagsList() 
    
{
        
this(10);
    }


    
public void add(String str) 
    
{
        ensureCapacity(size 
+ 1);
        data[size
++= str;
    }


    
public String get(int index) 
    
{
        
if (index < size)
            
return data[index];
        
else
            
return null;
    }


    
//为了提高效率,只将其置为null
    public boolean remove(String str) 
    
{
        
for (int index = size - 1; index >= 0; index--{
            
if (str.equals(data[index])) {
                data[index] 
= null;
                
return true;
            }

        }

        
return false;
    }

    
    
public boolean remove(int index)
    
{
        
if (index < data.length) {
            data[index] 
= null;
            
return true;
 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,如果您希望将富文本格式的HTML拆分成多个段落,并将每个段落的开放和闭合标签对分别存储在不同的数组,您可以尝试以下JavaScript函数。 ```javascript function splitHtmlTagsByParagraph(html) { const paragraphs = html.split(/<\/p>\s*<p[^>]*>/i); const result = []; for (let i = 0; i < paragraphs.length; i++) { const paragraph = paragraphs[i]; const tags = paragraph.match(/<[^>]+>/g) || []; const contents = paragraph.split(/<[^>]+>/g); if (tags.length !== contents.length - 1) { throw new Error(`Invalid HTML in paragraph ${i + 1}`); } const tagContentPairs = []; for (let j = 0; j < tags.length; j++) { const tag = tags[j]; const content = contents[j + 1]; const isOpeningTag = !tag.startsWith("</"); if (isOpeningTag) { tagContentPairs.push([tag, content, null]); } else { const openingTag = tagContentPairs.pop(); const openingTagName = openingTag[0].replace("<", "").replace(">", ""); const closingTagName = tag.replace("</", "").replace(">", ""); if (openingTagName === closingTagName) { openingTag[2] = content; tagContentPairs.push(openingTag); } else { throw new Error(`Mismatched tag: ${openingTagName} doesn't match ${closingTagName}`); } } } result.push(tagContentPairs); } return result; } ``` 使用示例: ```javascript const html = '<p>Hello <strong>world</strong>!</p><p>How are you?</p>'; const tagContentPairsByParagraph = splitHtmlTagsByParagraph(html); console.log(tagContentPairsByParagraph); // Output: [[["<p>", "Hello ", null], ["<strong>", "world", "</strong>"], ["</p>", "!", null]], [["<p>", "How are you?", "</p>"]]] ``` 上面的代码将会将包含在每个 `<p>` 标签文本拆分成多个数组,并将这些数组按照段落存储在 `tagContentPairsByParagraph` 数组。数组的每个元素都包含一个或多个子数组,每个子数组都包含三个值:开放标签、相应的文本闭合标签。如果标签未正确匹配,函数将会抛出一个错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值