html - 替换网页中的 'non-tagged'内容

标签 html ios regex parsing

我目前正在寻找一种替换网页中特定文本的方法,但我不想弄乱任何可能用作标记的内容(即 HTML 本身)。我研究了多种方法,包括匹配“<”和“>”字符(并忽略中间的内容),但不幸的是,当网页格式错误且它们不匹配或内容不匹配时,这种方法就会中断很差,或者在实际文本中嵌入了“<”或“>”。它也非常慢。

提取特定文本不是目标。相反,我需要用不同的文本替换它。

//编辑以使其更清楚(不确定为什么我对这个问题有两个 -1)。

1) 这是一个非常简单的例子

<head>
    <title>This is my website</title>
    <link rel="shortcut icon" href="//a.b.c">
    <meta name="twitter:card" content="summary">
    <meta property="og:type" content="website" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
        mystuff.ready(function () {    
            mystuff.using("snippets", function () {
                mystuff.snippets.initSnippetRenderer();
            });   
        });
    </script>    
</head>
<body class="question-page new-topbar">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <h1>This is piece of large text</h1>
    <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
    </ul>
</body>

当您打开浏览器时,您希望在浏览器中看到的内容如下(我们称之为“感兴趣的文本”):

This is a Large Piece of Text
* Coffee
* Tea
* Milk

因此,我感兴趣的关键是如何确定标签之外的内容(即感兴趣的文本并允许使用 RegEX 搜索和替换它)。

2). @Zaph - stringByReplacingOccurrencesOfString:withString:options:range 是不够的,因为它不能直接确定范围是什么。范围取决于文本是否包含在 HTML 标签中,或者负载是否由标签操作)- 请参见上面的“1”点。

例如,如果我直接替换文本“网站”,那么它将替换标题中的文本,但它也会错误地替换第二个元标记中的术语,这是不好的。

有什么想法,或者我可能想到的其他任何东西可以智能地与 HTML 负载而不是支持标签配合使用?

最佳答案

将正则表达式与后视断言和前视断言结合使用。

该示例将匹配的文本替换为自身,但包裹在邪恶的表情符号中。重点是演示匹配模式。使用 NSRegularExpression以便更好地控制替换。

解释:

(?<=>) Must be preceded with: >
\\S Must start with a non-whitespace character (the \ has to be escaped)
[^<>]+ Must consist of characters except < and >
(?=</) Must be followed by </

NSString *html = <question html>;

NSString *pattern = @"(?<=>)\\S[^<>]+(?=</)";
NSString *replacement = @"😈$0👿";
html = [html stringByReplacingOccurrencesOfString:pattern
                                       withString:replacement
                                          options:NSRegularExpressionSearch
                                            range:NSMakeRange(0, html.length)]
NSLog(@"html:\n%@", html);

输出:

<head>
    <title>😈This is my website👿</title>
    <link rel="shortcut icon" href="//a.b.c">
    <meta name="twitter:card" content="summary">
    <meta property="og:type" content="website" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
        mystuff.ready(function () {    
            mystuff.using("snippets", function () {
                mystuff.snippets.initSnippetRenderer();
            });   
        });
    </script>    
</head>
<body class="question-page new-topbar">
    <noscript><div id="noscript-padding"></div></noscript>
    <div id="notify-container"></div>
    <h1>😈This is piece of large text👿</h1>
    <ul>
        <li>😈Coffee👿</li>
        <li>😈Tea👿</li>
        <li>😈Milk👿</li>
    </ul>
</body>

关于html - 替换网页中的 'non-tagged'内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29589450/

相关文章:

iOS : How to reload a UITableView with a lot of cells without lagging the App?

iphone - iPhone 上 SQLite 数据库的光保护

Xcode : exclude matches from result set? 中的正则表达式

java - 正则表达式使用 ( ) 来搜索而不是指示组

javascript - 用变量和 anchor 替换正则表达式

javascript - 使用 jQuery 创建一个数组而不是从 dom 中获取的值的变量?

javascript - 使用 HTML5、JS、PHP 上传后将图像路径存储到 MySQL 数据库

php - 从包含 CSS 的 PHP/HTML 文件生成 PDF

ios - 点击单元格时 UILabel 向右移动

javascript - 我如何知道正在发生什么事件?