import Foundation
let pattern = #"""
(?:\G(?!\A)|<img)
(?s:.+?\bsrc=['"])\K
https?://.+?(?=/wp-content/)
"""#
let regex = try! NSRegularExpression(pattern: pattern, options: .allowCommentsAndWhitespace)
let testString = #"""
<img
src="https://www.example.com/de/wp-content/uploads/sites/1/2017/03/image.jpg"
data-srcset="
https://www.example.com/de/wp-content/uploads/sites/1/2017/03/img1.jpg 507w,
https://www.example.com/de/wp-content/uploads/sites/1/2017/03/img2.jpg 780w,
https://www.example.com/de/wp-content/uploads/sites/74/2017/03/img3.jpg 950w"
data-sizes="
(min-width: 80em) calc(0.5 * (100vw - (100vw- 57em))),
(min-width: 48em) calc(0.5 * (100vw - 5em)),
calc(100vw - 1em)"
alt="image" class="lazyload">
<img
src="https://www.example.com/de/wp-content/uploads/sites/1/2017/03/image.jpg"
data-srcset="
https://www.example.com/de/wp-content/uploads/sites/1/2017/03/img1.jpg 507w,
https://www.example.com/de/wp-content/uploads/sites/1/2017/03/img2.jpg 780w,
https://www.example.com/de/wp-content/uploads/sites/74/2017/03/img3.jpg 950w"
data-sizes="
(min-width: 80em) calc(0.5 * (100vw - (100vw- 57em))),
(min-width: 48em) calc(0.5 * (100vw - 5em)),
calc(100vw - 1em)"
alt="image" class="lazyload">
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"http://someotherdomain.com"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression