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><linkrel="shortcut icon"href="//a.b.c"><metaname="twitter:card"content="summary"><metaproperty="og:type"content="website" /><scriptsrc="//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><bodyclass="question-page new-topbar"><noscript><divid="noscript-padding"></div></noscript><divid="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"