Skip to content Skip to sidebar Skip to footer

Replacing 'non-tagged' Content In A Web Page

I'm currently looking at a method for replacing specific text within a web page, but I don't want to mess with anything that might be used as markup (i.e. HTML itself). I've looke

Solution 1:

Use a regular expression with Look-behind and Look-ahead assertion.

The example replaces the matched text with itself but wrapped in evil emoji. The point is to demonstrate the matching pattern. Use NSRegularExpression for more control over the replacements.

Explanation:

(?<=>) 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);

Output:

<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>

Post a Comment for "Replacing 'non-tagged' Content In A Web Page"