const regex = /(^| )3 [дД]($|[ .,!?:;])/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(^| )3 [дД]($|[ .,!?:;])', 'gm')
const str = `Могут быть строки вида
3 д панель
панель 3 д
панель 3 Д
плитка 3 д. (может быть любой знак препинания)
ул. Степана Разина 23 д 3 кв 3 (23 д Не должно замениться)
Если будет 223 д или 223 дом не должно заменяться.
Нужно что бы в строке "3 д" заменилось на "3д" ли "3д."
может быть любой знак препинания после "д".
Так же может быть большая "Д"
Помогите пожалуйста в регулярках не силен.`;
const subst = `$13д$2`;
// 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