import Foundation
let pattern = #"<script>.*(adsConfig).*<\/script>"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
<script>
var middleCount = 0;
var = {
nonLinear: {
url: "https://8245.digital/vpaid/?id=7875&type=nonlinear&dcc=1",
total: 0 ,
offset: 10 * 60,
label: { name: 'label', val: 'overlay', counter: true }
},
pre: {
maxImpressions: 2 ,
urls: ["https://8245.digital/vpaid/?id=5241&label=preroll&dcc=1&maximp=2&synconly=1"]
},
middle: {
pop: true,
maxTotalImp: 0 ,
maxImpressions: 1,
urls: ["https://8245.digital/vpaid/?id=21515&synconly=1&maximp=1&dcc=1&mid=6","https://8245.digital/vpaid/?id=5242&synconly=1&maximp=1&dcc=1&mid=6"]
,
getUrls: function () {
var u = middleCount && .middle.urls
? .middle.urls.slice(1)
: .middle.urls;
middleCount++;
return u;
},
label: { name: 'label', val: 'middleroll', counter: true }
},
confirmTimeout: 60 * 1000,
confirmOn: ['localhost', 'zombie-film.net'],
vast: {timeouts: {loading: 20000, starting: 20000, global: 120000}},
volume: 0.3,
tv: true,
suppressOnFakeFullScreen: true
};
</script>
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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