", withString:-6ren">
gpt4 book ai didi

html - 从字符串中剥离 HTML 标签

转载 作者:IT王子 更新时间:2023-10-29 04:57:05 24 4
gpt4 key购买 nike

如何从字符串中删除 HTML 标记以便输出干净的文本?

let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)

最佳答案

嗯,我试过你的函数,它在一个小例子上起作用了:

var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)

//output " My First Heading My first paragraph. "

你能举一个问题的例子吗?

Swift 4 和 5 版本:

var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)

关于html - 从字符串中剥离 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com