ios - iOS中HTML标签的字符串替换

标签 ios regex uiwebview nsstring

我正在解析 UIWebview 的字符串在 iOS .

我有一个数据 -

<p>The post <a rel="nofollow" href="http://www.someurl.com/bikes/triumph-motorcycles-plans-sell-2700-units-2015-16/">Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16</a> appeared first on <a rel="nofollow" href="http://www.someurl.com">MotorBuzz -  Car Bike News &amp; Reviews</a>.</p>

我想要的是删除文本的超链接。我不想删除文本我只想禁用文本的超链接 .

因此,对于上述数据,它应该看起来像 -
 <p>The post Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16 appeared first on MotorBuzz -  Car Bike News &amp; Reviews.</p>

所以请让我知道 中的任何正则表达式或任何字符串替换解决方案iOS/目标 -C .

谢谢。

最佳答案

您可以使用NSAttributedString .首先,您将 html 字符串转换为 NSAttributedString ,然后从中获取实际的字符串。

NSString *html = @"<p>The post <a rel=\"nofollow\" href=\"http://www.someurl.com/bikes/triumph-motorcycles-plans-sell-2700-units-2015-16/\">Triumph Motorcycles Plans To Sell Over 2700 Units By 2015-16</a> appeared first on <a rel=\"nofollow\" href=\"http://www.someurl.com\">MotorBuzz -  Car Bike News &amp; Reviews</a>.</p>";

   NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
                                                                      options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                           documentAttributes:nil error:nil];
    NSString *actualString= [attrString string];

但它会从字符串中删除所有 html 标签。

关于ios - iOS中HTML标签的字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057278/

相关文章:

ios - RestKit的动态映射能解决这个复杂的JSON映射吗?

ios - 使用 PHPhotoLibrary 保存照片时元数据丢失

ios - 循环中的 NSRangeException

Java正则表达式用特殊字符和句点替换特殊字符,一个或多个空格和句点

php - 去除尾随非单词字符

ios - 关闭应用程序时 webview 的 cookie 会发生什么变化?

javascript - IOS:如何检测用户在内容可编辑的 div 中更改 UIWebView 上的文本

ios - 如何使用图像获得扭曲的镜像效果?

Python 正则表达式引用与替换编号冲突

ios - 如何在 UIWebView 中打开一个链接