使用REGEX在Atom中搜索和替换外部标记

时间:2021-02-26 16:51:36

Using Atom, I'm trying to replace the outer tag structure for multiple different texts within a document. Also using REGEX, which I'm not versed enough to come up with my own solution

使用Atom,我正在尝试替换文档中多个不同文本的外部标记结构。还使用REGEX,我不够精通我自己的解决方案

HTML to be searched <span class="klass">Any text string</span>

要搜索的HTML 任何文本字符串

Replace it with <code>Any text string</code>

将其替换为任何文本字符串

My REGEX (<?span class="klass">)+[\w]+(<?/span>)

我的REGEX( )+ [\ w] +( )

Is there a wildcard to "keep" the [\w] part into the replaced result?

是否有通配符将[\ w]部分“保留”到替换结果中?

1 个解决方案

#1


2  

You can use a capture group to capture the text in between the <span> tags during the match, and then use it to build the <code> output you want. Try the following find and replace:

您可以使用捕获组在匹配期间捕获标记之间的文本,然后使用它来构建所需的输出。尝试以下查找和替换:

Find:

<span class="klass">(.*?)</span>

Replace:

<code>$1</code>

Here $1 represents the quantity (.*?) which we captured in the search. One other point, we use .*? when capturing between tags as opposed to just .*. The former .*? is a "lazy" or tempered dot. This tells the engine to stop matching upon hitting the first closing </span> tag. Without this, the match would be greedy and would consume as much as possible, ending only with the final </span> tag in your text.

这里$ 1代表我们在搜索中捕获的数量(。*?)。另外一点,我们使用。*?在标签之间捕获而不仅仅是。*。前者 。*?是一个“懒惰”或淬火的点。这告诉引擎在点击第一个结束 标记时停止匹配。如果没有这个,匹配将是贪婪的并且将尽可能多地消耗,仅以文本中的最终 标记结束。

#1


2  

You can use a capture group to capture the text in between the <span> tags during the match, and then use it to build the <code> output you want. Try the following find and replace:

您可以使用捕获组在匹配期间捕获标记之间的文本,然后使用它来构建所需的输出。尝试以下查找和替换:

Find:

<span class="klass">(.*?)</span>

Replace:

<code>$1</code>

Here $1 represents the quantity (.*?) which we captured in the search. One other point, we use .*? when capturing between tags as opposed to just .*. The former .*? is a "lazy" or tempered dot. This tells the engine to stop matching upon hitting the first closing </span> tag. Without this, the match would be greedy and would consume as much as possible, ending only with the final </span> tag in your text.

这里$ 1代表我们在搜索中捕获的数量(。*?)。另外一点,我们使用。*?在标签之间捕获而不仅仅是。*。前者 。*?是一个“懒惰”或淬火的点。这告诉引擎在点击第一个结束 标记时停止匹配。如果没有这个,匹配将是贪婪的并且将尽可能多地消耗,仅以文本中的最终 标记结束。