views:

536

answers:

2

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?

+1  A: 

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];
Chas. Owens
Thanks. I've changed the question since I can search a string rather than UIWebView. I need to append to these strings before they go into the UIWebView.
4thSpace
you can also use JavaScript and the DOM to modify the page after it has been loaded with document.write().
Chas. Owens
I tried the technique you mentioned. anchors doesn't seem to have any string. In the debugger, Summary is blank. It does have valid memory address but not sure what's in there.
4thSpace
You may need to use JavaScript to expand the return result from document.anchors out into something useful.
Chas. Owens
You have another snippet on what you mean by that?
4thSpace
A: 

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.

Chas. Owens