快速说明:我知道降价解析器不关心这个问题。这是为了 md 文件中的视觉一致性以及实验。
样品:
# this
##that
###or this other
目标:阅读每一行,如果 Markdown 标题在井号/主题标签后面没有空格,则添加一个,使其看起来像:
# this
## that
### or this other
我的非正则表达式尝试:
function inelegantFunction (string $string){
$array = explode('#',$string);
$num = count($array);
$text = end($array);
return str_repeat('#', $num-1)." ".$text;
}
echo inelegantFunction("###or this other");
// returns ### or this other
这有效,但它没有机制来匹配七个“#”的不太可能的情况。
无论功效如何,我都想弄清楚如何在 php 中使用正则表达式(如果重要的话,也可能是 javascript)。
临摹微笑