const regex = /^(?!##?\s)([^\/\|\@\"!]*?)(##|#[@?$%]{1,3}#|\$@?\$)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?!##?\\s)([^\\\/\\|\\@\\"!]*?)(##|#[@?$%]{1,3}#|\\$@?\\$)', 'gm')
const str = `## comment
# #comment
! Basic rules
##div
#@#div
##.ads
#@#.ads
###ads
#@##ads
example.com,~example.org##div
example.com,~example.org#@#div
example.com,~example.org##.ads
example.com,~example.org#@#.ads
example.com,~example.org###ads
example.com,~example.org#@##ads
! HTML filtering rules
! https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#html-filters
example.com##^.badstuff
example.com##^script:has-text(7c9e3a5d51cdacfc)
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#html-filtering-rules
example.org\$\$script[data-src="banner"]
example.org\$@\$script[data-src="banner"]
! CSS rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#cosmetic-css-rules
example.com#\$#div { visibility: hidden; }
example.com#@\$#div { visibility: hidden; }
example.com#\$#.textad { visibility: hidden; }
example.com#@\$#.textad { visibility: hidden; }
example.com#\$##textad { visibility: hidden; }
example.com#@\$##textad { visibility: hidden; }
! Extended CSS selectors
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#extended-css-selectors
example.com#?#div:has(> a[target="_blank"][rel="nofollow"])
example.com#@?#div:has(> a[target="_blank"][rel="nofollow"])
example.com#?#.banner:matches-css(width: 360px)
example.com#@?#.banner:matches-css(width: 360px)
example.com#?##banner:matches-css(width: 360px)
example.com#@?##banner:matches-css(width: 360px)
example.com#\$?#div:has(> span) { display: none !important; }
example.com#@\$?#div:has(> span) { display: none !important; }
example.com#\$?#.banner:has(> span) { display: none !important; }
example.com#@\$?#.banner:has(> span) { display: none !important; }
example.com#\$?##banner:has(> span) { display: none !important; }
example.com#@\$?##banner:has(> span) { display: none !important; }
! JavaScript rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#javascript-rules
! https://adguard.com/kb/general/ad-filtering/create-own-filters/#scriptlets
example.com#%#window.__gaq = undefined;
example.com#@%#window.__gaq = undefined;
! Scriptlet rules
example.com##+js(aopw, Fingerprint2)
example.com#@#+js(aopw, Fingerprint2)
example.org,example.com#%#//scriptlet("abort-on-property-read", "alert")
example.com#@%#//scriptlet("abort-on-property-read")`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions