- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢您花时间阅读本文,如果这有点令人困惑、需要补救或之前曾被问过(广泛搜索,结果有限),请立即道歉。
我正在使用过时的软件进行编辑,如果您熟悉 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.
假设您要替换每个 foo
与 bar
,但仅在部分内。这很容易通过使用回调函数作为 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/
我的应用将 SceneKit 内容的“页面”与图像和文本交替。当我从图像页面前进到新的 SceneKit 页面时,前一个 SceneKit 页面中的内容会短暂显示,然后被新内容替换。时髦。 我只使用一
我正在尝试处理(在 C# 中)包含一些数字数据的大型数据文件。给定一个整数数组,如何对其进行拆分/分组,以便如果下一个 n(两个或更多)是负数,则前一个 n 元素被分组。例如,在下面的数组中,应该使用
刚接触promises,研究过。所以我的代码和我的理解: sql.connect(config).then(function(connection) { return connection.req
目前我在 if (roobaf) block 中有一些代码,这取决于 foo 和 bar 是否为假。我可以在 block 内再次检查这些条件,但感觉像是不必要的代码重复。 if (foo) {
我是一名优秀的程序员,十分优秀!