I have a big string that contains several strings like the following:
http://website.com/image.jpg and I want to add ?w=100 to each of them.
So far I thought about creating a regular expression where every time I would encounter that string, it would replace it by the same string with the ?w=100 added to it.
Here is an extension of String to replace the string:
public func stringByReplacingMatches(matchRegEx: String, withMatch: String) -> String {
do {
let regEx = try NSRegularExpression(pattern: matchRegEx, options: NSRegularExpressionOptions())
return regEx.stringByReplacingMatchesInString(self, options: NSMatchingOptions(), range: NSMakeRange(0, self.characters.count), withTemplate: withMatch)
} catch {
fatalError("Error in the regular expression: \"\(matchRegEx)\"")
}
}
And this is how I use it:
myString.stringByReplacingMatches("https:\\/\\/website\\.com\\/[\\w\\D]*.jpg", withMatch: "https:\\/\\/website\\.com\\/[\\w\\D]*.jpg?w=100"))
Problem, this is what I get after replacing the string:
https://website.com/[wD]*.jpg?w=100
How can I solve my problem?