简体   繁体   English

正则表达式替换对美元符号

[英]Regex replace pair of dollar signs

I have combined MathJax and Markdown markup and therefore I need to replace all $$ with <span>$$</span> so that Markdown don't render $$_^... signs from MathJax. 我将MathJaxMarkdown标记组合在一起,因此我需要将所有$$替换为<span>$$</span>这样Markdown才不会呈现MathJax的$$ _ ^ ...符号。 Also all \\[ \\] must be replaced with <div>\\[ \\]</div> . 同样,所有\\[ \\]必须替换为<div>\\[ \\]</div>

I found similar question but it's not exactly what I need. 我发现了类似的问题,但这并不是我真正需要的。 I need to convert this 我需要转换这个

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

to this 对此

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

Probably what I need to to is just in regex 我可能需要做的只是正则表达式

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

somehow include and $$ signs (or \\[ \\] ) and just with $1 embed the text inside <span>$$1$</span> and adapt to PHP. 以某种方式包括和$$符号(或\\[ \\] ),仅用$1将文本嵌入<span>$$1$</span>并适应PHP。

You need to do it in two steps because the replacement texts are different. 您需要分两个步骤进行操作,因为替换文本不同。

First, replace the $..$ : 首先,替换$..$

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

Then, replace the \\[...\\] : 然后,替换\\[...\\]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM