UPE // Regex patch in NSRange and option flags.

This commit is contained in:
ShikiSuen 2022-03-10 10:35:28 +08:00
parent c3f5224113
commit a6e221e5d4
1 changed files with 2 additions and 2 deletions

View File

@ -22,8 +22,8 @@ extension String {
mutating func regReplace(pattern: String, replaceWith: String = "") { mutating func regReplace(pattern: String, replaceWith: String = "") {
// Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914 // Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914
do { do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive) let regex = try NSRegularExpression(pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines])
let range = NSRange(location: 0, length: self.utf16.count) let range = NSRange(self.startIndex..., in: self)
self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith) self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
} catch { return } } catch { return }
} }