Objective-C stringByReplacingOccurrencesOfString 与正则表达式
我正在尝试使用正则表达式拆分包含 html 代码的字符串:
NSString* regex = @"<.*?>";
NSString* html = @"<span class="test">Test1</span><span class="test">Test2</span><span class="test">Test3</span><span class="test">Test4</span>";
html = [html stringByReplacingOccurrencesOfString:regex withString:@""];
我想删除 span 标签。
有什么想法吗?
I'm trying to split my string containing html code with a regex expression:
NSString* regex = @"<.*?>";
NSString* html = @"<span class="test">Test1</span><span class="test">Test2</span><span class="test">Test3</span><span class="test">Test4</span>";
html = [html stringByReplacingOccurrencesOfString:regex withString:@""];
I want to delete the span-tags.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能可以使用 此方法:
在上面链接的文档中检查您可能需要哪些选项。
You could probably do something like this with this method:
Check what options you may need in the documentation in the link above.
如果您的输入是 HTML,则使用 HTML PARSER。
用正则表达式“解析” HTML 是徒劳的。请注意,SO 上有很多问题描述了 iO/OSX 上的 HTML 解析。
If your input is HTML, the use an HTML PARSER.
"parsing" HTML with a regular expression is an exercise in futility. Note that there are plenty of questions on SO that describe HTML parsing on iO/OSX.
这只是删除了 <和>字符以及它们之间的所有内容,我认为这就足够了:
This just removes < and > characters and everything between them, which I suppose is sufficient: