$str = '1s2.2s2.3p6.(3D).5d';
$parts = explode('.', $str);
foreach($parts as $key => $part) {
$part = preg_replace('/(\d[a-z])(\d)/i', '$1<sup>$2</sup>', $part);
$part = preg_replace('/\((\d)(.*)\)/', '(<sup>$1</sup>$2)', $part);
$parts[$key] = $part;
}
$out = implode($parts);
// 1s<sup>2</sup>2s<sup>2</sup>3p<sup>6</sup>(<sup>3</sup>D)5d