NSRegularExpression:用< a>替换网址文字标签 [英] NSRegularExpression: Replace url text with <a> tags

查看:72
本文介绍了NSRegularExpression:用< a>替换网址文字标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用UIWebView来设计来自twitter的帖子。有些推文当然包含网址,但不包含< a> 标记。我可以提取网址,但是我不知道如何添加< a> 标记并将其放回到推文中。然后,我将使用相同的方法添加@usernames和#hashtags的链接。以下是我当前代码的示例:

I am currently using a UIWebView to stylize posts from twitter. Some tweets of course contain URL's, but do not contain the <a> tags. I am able to pull out the URL, however I am not sure how to add the <a> tags and place back into the tweet. I will then use the same approach here to add links to the @usernames and #hashtags. Here is an example of my current code:

NSString *tweet = @"Sync your files to your Google Docs with a folder on your desktop.  Like Dropbox.  Good choice, Google storage is cheap. http://ow.ly/4OaOo";

NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»""‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];

NSString *match = [tweet substringWithRange:[expression rangeOfFirstMatchInString:tweet options:NSMatchingCompleted range:NSMakeRange(0, [tweet length])]];
NSLog(@"%@", match);// == http://ow.ly/4OaOo

最后,我想要最后的字符串看起来像这样:

Ultimately, I would like the final string to look like this:

将您的文件同步到Google桌面上有文件夹的文档。就像Dropbox一样。不错的选择,谷歌存储很便宜。< a href =http://ow.ly/4OaOo>http://ow.ly/4OaOo</a>

我们非常感谢任何帮助。

Any help would be much appreciated.

推荐答案

这是一个objective-c版本:

And here is an objective-c version:

NSString *regexToReplaceRawLinks = @"(\\b(https?):\\/\\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|])";   

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexToReplaceRawLinks
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

NSString *string = @"Sync your files to your Google Docs with a folder on your desktop.  Like Dropbox.  Good choice, Google storage is cheap. http://ow.ly/4OaOo";

NSString *modifiedString = [regex stringByReplacingMatchesInString:string
                                                           options:0
                                                             range:NSMakeRange(0, [string length])
                                                      withTemplate:@"<a href=\"$1\">$1</a>"];

NSLog(@"%@", modifiedString);






之前我做过类似的事,但我用过javascript来做到这一点。加载视图后,使用委托方法 webViewDidFinishLoad ,然后注入javascript:


I did something like this before, but I used javascript to do it. When the view has loaded, use the delegate method webViewDidFinishLoad, and inject javascript:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *jsReplaceLinkCode = 
        @"document.body.innerHTML = " 
        @"document.body.innerHTML.replace("
            @"/(\\b(https?):\\/\\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|])/ig, "
            @"\"<a href='$1'>$1</a>\""
    @");";

    [webVew stringByEvaluatingJavaScriptFromString:jsReplaceLinkCode];
} 

这是非Objective-c nsstring引用版本中的javascript调用:

Here's the javascript call in a non objective-c nsstring quotes version:

document.body.innerHTML = document.body.innerHTML.replace(
      /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, 
      "<a href='document.location=$1'>$1</a>"
);

正则表达式并不完美,但会捕获大部分链接。

The regex is not perfect but will catch most of the links.

这篇关于NSRegularExpression:用&lt; a&gt;替换网址文字标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆