【发布时间】:2019-05-25 21:19:06
【问题描述】:
快速说明: 我知道 Markdown 解析器不关心这个问题。这是为了 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)。
【问题讨论】:
-
所以您基本上是在检查
^#+\S并想在那里添加一个空格? -
我怀疑是的。我尝试过的是 '/#(?
[^\s#]+)/' 但是,使用正则表达式,我总是在黑暗中导航,希望能做得更好。
标签: php regex pcre regex-lookarounds regex-group