const regex = /redb?\.(?:(?<dpi>dpi)|(?<port>port)|fb_width|fb_height)="\K\w+/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('redb?\\.(?:(?<dpi>dpi)|(?<port>port)|fb_width|fb_height)="\\K\\w+', 'gm')
const str = `bst.instance.red.port="5555"
bst.instance.red.dpi="240"
bst.instance.red.fb_height="900"
bst.instance.red.fb_width="1600"
bst.instance.red.first_boot="0"
bst.instance.redb.port="2000"
bst.instance.redb.dpi="120"
bst.instance.redb.fb_height="300"
bst.instance.redb.fb_width="800"
bst.instance.redb.first_boot="0"
bst.instance.redc.port="2000"
bst.instance.redc.dpi="120"
bst.instance.redc.fb_height="300"
bst.instance.redc.fb_width="800"
bst.instance.redc.first_boot="0"`;
const subst = `${dpi:+YYY:}${port:+WWW:}`;
// 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