const regex = /^(basedir[ ]*=[ ]*")(.)(:[\/\\])([a-zA-Z0-9]*)([\/\\]?.*)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(basedir[ ]*=[ ]*")(.)(:[\\\/\\\\])([a-zA-Z0-9]*)([\\\/\\\\]?.*)$', 'gm')
const str = `[mysqld]
#skip-innodb
# The TCP/IP Port the MySQL Server will listen on
port=3306
max_allowed_packet=16M
#Path to installation directory. All paths are usually resolved relative to this.
#basedir="C:/Program Files/MySQL/MySQL Server 5.1/"
basedir="C:/MySQL/"
#Path to the database root
#datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/"
datadir="C:/MySQLData/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8`;
const subst = `$1Z$3Here$5`;
// 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