Это не лучшее решение, но оно будет работать.
$re = '/
(.*?) # text before bBB...eBB
(\[(\w+?)\].*?\[\s*\/\3\]) # bBB...eBB
|
(.*?$) # text after last bBB..eBB
/xui';
$string = "beginOfS [b]Hello, World![/b] in either the color [blue]test lorem [yel]ipsum[/yel] dolorem [/blue] or [red]test[/red] endOfS";
echo preg_replace($re, '<b>\1\4</b>\2', $string);
// $nMatches = preg_match_all($re, $string, $aMatches);
Возврат:
<b>beginOfS </b>[b]Hello, World![/b]<b> in either the color </b>[blue]test lorem [yel]ipsum[/yel] dolorem [/blue]<b> or </b>[red]test[/red]<b> endOfS</b>