正则表达式取代对美元符号

时间:2012-06-25 08:54:22

标签: php regex markdown mathjax

我结合了 MathJax Markdown 标记,因此我需要将所有$$替换为<span>$$</span>,以便Markdown不会渲染$来自MathJax的$ _ ^ ......标志。此外,所有\[ \]必须替换为<div>\[ \]</div>

我发现了类似的question,但这并不是我需要的。我需要转换这个

This is $some$ math \[equation\] which I $like$.

到这个

This is <span>$some$</span> math <div>\[equation\]</div> which I <span>$like$</span>.

我需要的只是在正则表达式

text = text.replace(/\$.*?\$/g, "meow");

以某种方式包含和$$个符号(或\[ \]),只需$1将文本嵌入<span>$$1$</span>并适应PHP。

1 个答案:

答案 0 :(得分:1)

您需要分两步完成,因为替换文本不同。

首先,替换$..$

$text = preg_replace('/\$.*?\$/', '<span>\0</span>', $text);

然后,替换\[...\]

$text = preg_replace('/\\\\\[.*?\\\\\]/', '<div>\0</div>', $text);
相关问题