Regex,如果文本被$$包围,则可以剥离文本中的所有标记


Regex to strip all tags in a text if it is surrounded by $$

如果文本中的所有标记位于两个连续$$之间,我需要regex来剥离它。连续两个$$形成一个区块。

例如:-以下文本

This is <outtag>just</outtag> a test from $$outside and
<intag>from</intag> $$ inside and again <outtag>I am </outtag>inside out $$so I have
<intag>to go <'intag>in$$ go outside.

必须删除intag,因为它位于由两个连续$$组成的块内。我正在尝试将其与regex 进行匹配

('$'$.*?)<(.+?)>(.*?)<'/'2>.*?'$'$

但我不知道如何重复$$中出现的标签,也不知道如何制作两个连续$$的块。

@nauriel我有一个mathjax代码,它由markdown处理,如果有两个下划线或其他markdown字符,就会在其中放置标记。

是的,我总是有一个偶数$$。

@sln只是标记<intag></intag>,而不是标记的全部内容。

非常简单且简洁:

$input = 'This is <outtag>just</outtag> a test from $$outside and
<intag>from</intag> $$ inside and again <outtag>I am </outtag>inside out $$so I have
<intag>to go </intag>in$$ go outside.';
$output = preg_replace_callback('/'$'$.*?'$'$/s', function($m) {
    return preg_replace('/<(.+?)>(.*?)<'/'1>/s', '$2', $m[0]);
}, $input);
echo $output;