The original regex is:
/.*advert.*|.*adserv.*|.*adsys.*|((^|\.|-)ad(s|v)*\d*(\.|-).+\..+)/
Let's break it down and optimize:
.*advert.*|.*adserv.*|.*adsys.*:
These three parts use .* at the beginning and end, which means they match if the keywords "advert", "adserv", or "adsys" appear anywhere in the string.
The .* at the beginning is particularly inefficient, especially on long strings that don't match early. Regex engines are good at finding substrings, so keyword is usually enough if you just want to test for its presence.
These can be combined and made more efficient: ad(?:vert|serv|sys)
ad is a common prefix.
(?:...) creates a non-capturing group.
vert|serv|sys lists the alternatives.
This will match "advert", "adserv", or "adsys" anywhere in the string.
((^|\.|-)ad(s|v)*\d*(\.|-).+\..+):
This part is more complex and looks for patterns like ad-something.domain.com, .ads.domain.com, adv123.domain.com.
(^|\.|-): Matches the start of the string (^), or a literal dot (\.), or a hyphen (-). This effectively anchors the pattern to the beginning of a domain/subdomain component.
ad: Literal "ad".
(s|v)*: Zero or more occurrences of "s" or "v" (e.g., "s", "v", "sv", "vs", "" - empty).
\d*: Zero or more digits.
(\.|-): A literal dot or a hyphen.
.+\..+: At least one character, followed by a literal dot, followed by at least one character. This ensures a structure like name.tld or sub.domain.tld after the ad-like prefix.
The outermost ((...)) is a capturing group. If you don't need to capture this whole match, it can be made non-capturing (?:...). Similarly for inner groups if their specific captures aren't used. For optimization, non-capturing groups are generally preferred.
Optimized Regex Construction:
Combining the optimized parts:
Part 1 (keywords): ad(?:vert|serv|sys)
Part 2 (pattern): (?:(?:^|\.|-)ad(?:s|v)*\d*(?:\.|-).+\..+)
* Outer (?:...) makes the whole alternative non-capturing.
* Inner groups also made non-capturing: (?:^|\.|-), (?:s|v), (?:\.|-).
The full optimized regex becomes:
ad(?:vert|serv|sys)|(?:(?:^|\.|-)ad(?:s|v)*\d*(?:\.|-).+\..+)
Recommended Flags:
For matching ad-related domains/URLs, case-insensitivity is almost always desired. So, you should use the i flag.
Reduced Redundancy and Improved Specificity:.*keyword.* was replaced by ad(?:vert|serv|sys). This is much faster because the regex engine doesn't have to backtrack through the greedy .*. It directly looks for "ad" followed by "vert", "serv", or "sys".
Non-Capturing Groups:(?:...) is used for grouping instead of (...) where the captured text is not needed. This can offer a slight performance improvement and simplify group indexing if captures were used elsewhere.
Clarity: The structure A|B where A and B are distinct, optimized patterns is clearer.
Why it's better:
Performance: The removal of leading .* is the biggest win. Matching advert is much faster than .*advert.
Readability (Arguably): While regex can be dense, factoring common parts and using non-capturing groups appropriately can make the intent clearer to those familiar with these optimizations.
If you need to preserve the original capturing group structure:
The original regex had the fourth alternative ((^|\.|-)ad(s|v)*\d*(\.|-).+\..+) as capturing group 1, with its internal parts being groups 2, 3, and 4. If you rely on these exact capture groups, the optimization would be:
In this version, the second main alternative is still capturing group 1. The groups (^|\.|-), (s|v), and (\.|-) within it would be groups 2, 3, and 4 respectively. (Note: (s|v)* makes (s|v) group 3, capturing only the last 's' or 'v' if multiple are present, like in "adsv"; the * applies to the group. \d* is not grouped).
The original (s|v)* is slightly ambiguous in how different engines might number captures if s or v appears multiple times, but typically it's the capture of the (s|v) group itself.
For pure matching (true/false), the non-capturing version is generally best: