zoukankan      html  css  js  c++  java
  • iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法

    content是根据网址获得的网页源码字符串

    - (NSString *)changeToString:(NSString *)content
    {
        NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|
    "
                                                                                        options:0
                                                                                          error:nil];
        
        content = [regularExpretion stringByReplacingMatchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length) withTemplate:@"-"];         //  替换所有html和换行匹配元素为"-"
        
        regularExpretion = [NSRegularExpression regularExpressionWithPattern:@"-{1,}" options:0 error:nil] ;
        content = [regularExpretion stringByReplacingMatchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length) withTemplate:@"-"];          //  把多个"-"匹配为一个"-"
        
        //  根据"-"分割到数组
        NSArray *arr=[NSArray array];
        content = [NSString stringWithString:content];
        arr =  [content componentsSeparatedByString:@"-"];
        NSMutableArray *marr=[NSMutableArray arrayWithArray:arr];
        [marr removeObject:@""];
        NSMutableString *string = [[NSMutableString alloc] init];
        for (int i = 0; i < arr.count; i++) {
            [string appendString:[NSString stringWithFormat:@"%@",arr[i]]];
        }
        return  string;
    }
    
  • 相关阅读:
    面向对象
    用JS添加和删除class类名
    偶然
    js-cookie的用法
    eleemnt-ui修改主题颜色
    router.go,router.push,router.replace的区别
    vue生产环境清除console.log
    特别关心
    echart
    20182330魏冰妍_预备作业
  • 原文地址:https://www.cnblogs.com/iyou/p/4858655.html
Copyright © 2011-2022 走看看