gpt4 book ai didi

javascript - 如何使用正则表达式在先前搜索的结果中搜索和替换?

转载 作者:行者123 更新时间:2023-11-29 14:54:30 25 4
gpt4 key购买 nike

感谢您花时间阅读本文,如果这有点令人困惑、需要补救或之前曾被问过(广泛搜索,结果有限),请立即道歉。

我正在使用过时的软件进行编辑,如果您熟悉 HomeSite 5,它允许使用脚本。

我的难题如下:

I would like to isolate multiple selections of text. I am currently doing this using a (long-winded) regex that captures all of the content following a certainspecifically set date (in this instance "2030-12-31") until it reaches a certain tag (in this instance ]]<content>). Thus far I have managed.

I then would like, within only those previously found selections of text, to remove all of the <span> tags it contains. However, I would like the <span> tags in other sections of text to remain (for example those assigned earlier dates).

我可以单独执行这两个功能,隔离特定部分或删除所有 <span>标签,我觉得只有一个我不知道的链接可以让我在另一个中运行一个。

再一次,如果答案很简单,我们深表歉意;我对脚本和正则表达式的了解充其量是有限的。我一直在使用 Jscript 完成我的大部分工作,但是我不确定 HomeSite 是否接受其他格式 - 我愿意接受多种解决方案!

TLDR: Search and replace only within certain selections, as specified by immediately preceeding regex.

编辑 1:请参阅下面用于隔离所需部分的表达式。首先是整个表达式。第二个是捕获内容的容器。:

/<version recordId="([0-9]{4,})" start="2030-12-31"([^>]*)>([^<]*)<title><!\[CDATA\[<span class="uk">([^<]*)<\/span>\]\]><\/title>([^<]*)<number><!\[CDATA\[<span class="uk">([0-9]{1,3})\.<\/span>\]\]><\/number>([^<]*)<content><!\[CDATA\[([^]]*)\]\]><\/content>([^<]*)<\/version>/g;

..<content><!\[CDATA\[([^]]*)\]\]></content>..

其中我希望修改如下:

<span class="uk">content</span>
content

现在我已经在公共(public)场合输入了它,我知道它是一种多么恐怖的正则表达式表演,我向 stackoverflow 的优秀编码人员道歉,甚至不得不看它!

编辑 2:请参阅下面的所需输出示例:

<version recordId="1234" start="2012-01-01"><stuffhere...<content><![CDATA[[
<span class="uk">content1</span>
<span class="uk">content2</span>
]]</content>
</version>
<version record="4231" start="2030-12-31"><stuffhere...<content><![CDATA[[
<span class="uk">content1</span>
<span class="uk">content2</span>
]]</content>
</version>

成为

<version recordId="1234" start="2012-01-01"><stuffhere...<content><![CDATA[[
<span class="uk">content1</span>
<span class="uk">content2</span>
]]</content>
</version>
<version record="4231" start="2030-12-31"><stuffhere...<content><![CDATA[[
content1
content2
]]</content>
</version>

n.b:感谢 Hannele 之前的格式更正。

最佳答案

使用回调函数 String.replace()

String.replace() 的第二个参数方法(替换文本)可以指定为回调函数。这个回调函数又可以有另一个 replace()称呼。通过这种方式,您可以轻松处理节内文本。下面是一个演示此技术的示例。

鉴于此示例文本:

之前:

blah foo? foo blah foo, foo.
<section1>blah foo? foo blah foo, foo.</section1>
blah foo? foo blah foo, foo.
<section2>blah foo? foo blah foo, foo.</section2>
blah foo? foo blah foo, foo.

假设您要替换每个 foobar ,但仅在部分内。这很容易通过使用回调函数作为 String.replace() 的替换参数来完成。像这样的方法:

function f1(text) {
var re1 = /<section(\d+)>[\S\s]*?<\/section\1>/g;
var re2 = /foo/ig;
text = text.replace(re1,
function(m0, m1){
return m0.replace(re2, 'bar');
});
return text;
}

当找到模式匹配时,replace()方法调用回调函数并在第一个参数中传递整个匹配项(在上面的示例中,我将其命名为:"m0")。如果正则表达式有捕获组,则每个组的匹配文本将在以下参数中传递(在这种情况下,只有一个捕获组,我将此参数命名为:"m1" - 请注意,此参数是未被函数使用)。

下面是经过上述函数处理后的示例文本:

之后:

blah foo? foo blah foo, foo.
<section1>blah bar? bar blah bar, bar.</section1>
blah foo? foo blah foo, foo.
<section2>blah bar? bar blah bar, bar.</section2>
blah foo? foo blah foo, foo.

关于javascript - 如何使用正则表达式在先前搜索的结果中搜索和替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20146649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com