0

我正在尝试在 HTML 字符串中查找一些文本正则表达式匹配项,并将其替换为特殊标记。在下面的示例字符串中,我想找到单词swiftsoup,并将其替换为<b>swiftsoup</b>,但排除所有属性中的所有匹配项,如urlid="swiftsoup"hrefurl。

// example string
<p>swiftsoup is awesome, but I don't know how to solve with <a id="swiftsoup" href="https://github.com/scinfu/swiftsoup">swiftsoup</a> or other. Love swiftsoup even so.</p>

下面的 SwiftSoup 代码当然不起作用,因为ownText()totext()不是一个变异函数,不能处理未使用的结果replacingOccurrences(of:with:)

let h = #"<p>swiftsoup is awesome, but I don't know how to solve with <a id="swiftsoup" href="https://github.com/scinfu/swiftsoup">swiftsoup</a> or other. Love swiftsoup even so.</p>"#

let p = try! SwiftSoup.parse(h).select("p").first()!

p.ownText().replacingOccurrences(of: "swiftsoup", with: "<b>swiftsoup</b>")
           ^~~~~~

也许正则表达式html()可能会有所帮助,但我不知道如何在属性值中保留匹配项:

extension String {
    func markUpSwiftSoup() -> String {
        var selfResult = self
        let selfAsNSString = self as NSString

        if let regex = try? NSRegularExpression(pattern: "swiftsoup") {

            let range = NSRange(location: 0, length: selfAsNSString.length)
            regex.matches(in: self, options: [], range: range).forEach {

                let match = selfAsNSString.substring(with: $0.range)
                selfResult = selfResult.replacingOccurrences(of: match, with: "<b>\(match)</b>")
            }

            return selfResult

        } else {
            return self
        }
    }
}

var pHTML = try! p.html()
p.html(pHTML.markUpSwiftSoup())

我试图得到的结果是:

<p><b>swiftsoup</b> is awesome, but I don't know how to solve with <a id="swiftsoup" href="https://github.com/scinfu/swiftsoup"><b>swiftsoup</b></a> or other. Love <b>swiftsoup</b> even so.</p>

提前致谢!

4

0 回答 0