String.replacingOccurrencesのオプションに.regularExpressionを指定します。また、後方参照は\1や{1}でなく$1です。以下、twitterのプロフィール画像URLを置換する例です。
extension String {
func twttrPhoto() -> String {
return replacingOccurrences(
of: "^https?://(.+)_normal(.+)$",
with: "https://$1$2",
options: .regularExpression,
range: self.range(of: self))
}
}
let url = "http://pbs.twimg.com/profile_images/1610656772/nTotani_normal.jpg"
url.twttrPhoto()
// => https://pbs.twimg.com/profile_images/1610656772/nTotani.jpg
先頭がhttpsになり、_normalが取り除かれています。