Updates href values for a limited collection of links on a page As much as it hurts, I've been tasked with writing a script that prepends integration, QA, etc. environment segments to anchor href attributes that link to a series of sites within two specific domains.

Since we can't be certain of adherence to the requirement that content authors always use production URLs (without these segments) this script covers cases where the wrong segment is present in a particular downstream environment.

We do know the domains and finite list of environments.

/*
    Updates URL environment segments in page links (anchor hrefs)

    1. Determines the current environment by parsing location.hostname.
    2. Selects a list of links with absolute URIs or network-path references:
    (`a[href^="http"], a[href^="//"]`).
    3. Loops over the list of links and takes action only on items that contain
    _domainone.com_ or _domaintwo.com_ in the `href` attribute.
        a. If environment is production, removes the environment segment
        b. If downstream environment, adds or updates environmnet segment to
        match the current environment.

    Currently supports domainone.com and domaintwo.com domains. To
    add additional domains, update the `domainRegEx` variable. This script also
    assumes a finite list of environment names (authoring, integration,
    localhost, qa, and sys). To support additional environments, update the
    `envRegEx` variable.
*/
(function(window) {
    window.addEventListener('DOMContentLoaded', () =&gt; {
        const domainRegEx = /(domainone\.com)|(domaintwo\.com)/i;
        const envRegEx = /authoring\.|integration\.|localhost\.|qa\.|sys\./i;
        const environment = location.hostname.match(envRegEx);
        const links = document.querySelectorAll('a[href^="http"], a[href^="//"]');

        for (let i = links.length - 1; i &gt; -1; i--) {
            const link = links[i];
            const href = link.getAttribute('href');
            if (domainRegEx.test(href)) {
                // If production, remove any environment segments ...
                if (environment === null) {
                    link.setAttribute('href', href.replace(envRegEx, ''));
                } else {
                    // ... else if downstream environment, ensure correct environment segment.
                    if (envRegEx.test(href)) {
                        link.setAttribute('href', href.replace(envRegEx, `${environment[0]}`));
                    } else {
                        link.setAttribute('href', href.replace(domainRegEx, `${environment[0]}$1`));
                    }
                }
            }
        }
    });
}(window));
There's no need to wrap this all in an IIFE. You aren't introducing any variables in the top scope, so no need to prevent them from leaking. Though it will become more useful when you follow my advice of introducing some helper functions. But even then there's no need to safe-guard the window global (as you aren't worried about the location and document globals).
Why are you running the loop backwards? Better use a looping construct that's meant for dealing with lists, like the for-of loop.
The environment variable contains a result of a regex match and is used like so:

// If production, remove any environment segments ...
if (environment === null) {


If you would translate the regex match into an actual environment name, the code would become much more self-explanatory:

if (environment === "production") {

For changing the link href value, I'd suggest extracting helper functions like stripEnvironment() and replaceEnvironment(), which could be used as:

link.setAttribute('href', stripEnvironment(href));
link.setAttribute('href', replaceEnvironment(href, environment));

If you want to have the domains and environments lists to be easily extensible, regex is probably not the best choice. Better to define them as arrays of strings which you can later transform into regexes using the RegExp constructor.
Your regexes look a bit too loose. The /sys\./ pattern would also match linksys.. You should at least anchor them at word boundary using \b.