I have an NSString with web links in it. I need to find all web links in the string starting with http://. What is the best way to do this?
The method
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
gives you the ability to run custom JavaScript. The JavaScript might have access to the DOM, if so it should be simple to extract all of the anchor tags and return them.
Of course, I have only written the tiniest of iPhone apps and am not very familiar with JavaScript or the DOM, so I could be way off base here.
Looking at the DOM and JavaScript it may be as easy as
anchors = [htmlview_object stringByEvaluatingJavaScriptFromString @"document.anchors"];
but, again, I have don't really know what I am talking about.
Try this:
javascript = @"function return_links() {\n"
" var a = new Array;\n"
" for (i = 0; i < document.anchors.length; i++) {\n"
" a.push(document.anchors[i].href);\n"
" }\n"
" return a.join(\"\\n\");\n"
"}\n"
"return_links();\n";
links = [htmlview_object stringByEvaluatingJavaScriptFromString javascript];
The best way is to find links in an NSString an HTML Parser, see Parsing HTML on the iPhone. If you must use a regex (and see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why you shouldn't), PCRE seems to be available for the iPhone. A common, and flawed, regex for links is /href="(.*?)"/
.
Of course, the best parser available is probably the native html viewing widget, and since it gives you the ability to run JavaScript (see my original answer to your original question) you should be able to extract the anchors from the DOM (document.anchors) and write/rewrite whatever you need to using document.write() and the other DOM methods.