const regex = /\$([^{("\\'\/])([a-zA-Z0-9_]*)([^}\/ \t'"\n.\]:]*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\$([^{("\\\\\'\\\/])([a-zA-Z0-9_]*)([^}\\\/ \\t\'"\\n.\\]:]*)', 'gm')
const str = `#!/usr/bin/env bash
# version 0.1.0
function generate_secret()
{
( umask 0077 ; dd if=/dev/urandom bs=24 count=1 2>/dev/null | base64 -w0 > "\$1" )
}
if [[ -n \$SRS_DOMAINNAME ]]
then
NEW_DOMAIN_NAME="\$SRS_DOMAINNAME"
elif [[ -n \$OVERRIDE_HOSTNAME ]]
then
NEW_DOMAIN_NAME="\${OVERRIDE_HOSTNAME#*.}"
elif [[ -n \$DOMAINNAME ]]
then
NEW_DOMAIN_NAME="\$DOMAINNAME"
else
NEW_DOMAIN_NAME=\$(hostname -d)
fi
sed -i -e "s/localdomain/\${NEW_DOMAIN_NAME}/g" /etc/default/postsrsd
POSTSRSD_SECRET_FILE='/etc/postsrsd.secret'
POSTSRSD_STATE_DIR='/var/mail-state/etc-postsrsd'
POSTSRSD_STATE_SECRET_FILE="\${POSTSRSD_STATE_DIR}/postsrsd.secret"
if [[ -n \$SRS_SECRET ]]
then
( umask 0077 ; echo "\$SRS_SECRET" | tr ',' '\\n' > "\$POSTSRSD_SECRET_FILE" )
else
if [[ \$ONE_DIR -eq 1 ]]
then
if [[ ! -f \$POSTSRSD_STATE_SECRET_FILE ]]
then
install -d -m 0775 "\$POSTSRSD_STATE_DIR"
generate_secret "\$POSTSRSD_STATE_SECRET_FILE"
fi
install -m 0400 "\$POSTSRSD_STATE_SECRET_FILE" "\$POSTSRSD_SECRET_FILE"
elif [[ ! -f \$POSTSRSD_SECRET_FILE ]]
then
generate_secret "\$POSTSRSD_SECRET_FILE"
fi
fi
if [[ -n \$SRS_EXCLUDE_DOMAINS ]]
then
sed -i -e "s/^#\\?SRS_EXCLUDE_DOMAINS=.*\$/SRS_EXCLUDE_DOMAINS=\$SRS_EXCLUDE_DOMAINS/g" /etc/default/postsrsd
fi
/etc/init.d/postsrsd start
`;
const subst = `${$1$2$3}`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', 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 JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions