<?
$repl="Г"; // "<span style='color:red'>Г</span>";
$text = "аг бг вг гг";
$RE1= "(?<=[абв])г";
$RE2= "(?<=\\s[абв])г";
$RE3= "(?<=\\s(?:а|б|в))г";
echo "\n";
echo preg_replace("/$RE2/", $repl, $text); // fail, expected аг бГ вГ гг echo "\n";
?>
PD8KJHJlcGw9ItCTIjsgLy8gIjxzcGFuIHN0eWxlPSdjb2xvcjpyZWQnPtCTPC9zcGFuPiI7CiR0ZXh0ID0gItCw0LMg0LHQsyDQstCzINCz0LMiOwokUkUxPSAiKD88PVvQsNCx0LJdKdCzIjsKJFJFMj0gIig/PD1cXHNb0LDQsdCyXSnQsyI7CiRSRTM9ICIoPzw9XFxzKD860LB80LF80LIpKdCzIjsKZWNobyBwcmVnX3JlcGxhY2UoIi8kUkUxLyIsICRyZXBsLCAkdGV4dCk7IC8vIG9rCmVjaG8gIlxuIjsKZWNobyBwcmVnX3JlcGxhY2UoIi8kUkUyLyIsICRyZXBsLCAkdGV4dCk7IC8vIGZhaWwsIGV4cGVjdGVkINCw0LMg0LHQkyDQstCTINCz0LMKZWNobyAiXG4iOwplY2hvIHByZWdfcmVwbGFjZSgiLyRSRTMvIiwgJHJlcGwsICR0ZXh0KTsgLy8gb2sKPz4=