I am trying to replace words in string using regex in Swift 5.
my code:
let text = "hi avoid avoid avoid"
let regex = "/(avoid|another word|word3)/gi"
let str = text.replacingOccurrences(of: regex, with: "", options: [.regularExpression])
print(str) // will print: "hi avoid avoid avoid"
print(str) // will print: "hi "
So the regex should remove any avoid, another word or word3 words from the string
any help?
let myString = "hi avoid avoid avoid"
let regex = try! NSRegularExpression(pattern: "(avoid|another word|word3)",
options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, myString.count)
let modString = regex.stringByReplacingMatches(in: myString, options: [],
range: range, withTemplate: "")
print(modString)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.