删除所有HTML标签,如
或从字符串.我使用下面的代码,但它不起作用.
var content = "test result
"; // My String content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)
但它不会从字符串中删除所有HTML标记.
var content = "test result
"; // My String let a = content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)
a将是: test result
let b = a.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil)
b现在是: test result
这也将照顾<
等等.没有魔力.找出你需要的东西,然后写出适当的RegEx.
经Swift 4测试:删除所有HTML标记并解码实体
提供更稳定的结果
extension String { public var withoutHtml: String { guard let data = self.data(using: .utf8) else { return self } let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [ .documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue ] guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else { return self } return attributedString.string } }
使用在Swift 3.0中在Playground上测试的“跟随扩展”
extension String { var withoutHtmlTags: String { return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) } }
用法
let result = "HTML Tags Contain String".withoutHtmlTags