更新
我找到了一个纯基于 X-Ray 的解决方案,无需<br>
在使用 X-Ray 之前替换 html 中的标签(请参阅下面的原始解决方案)。
这样,您将使用 X-Ray 的filter
函数以及相互嵌入 X-Ray 函数(某种嵌套)。
首先,我们将使用为 X-Ray 定义的<br>
自定义过滤函数(称为)替换原始 html 中的标签。replaceLineBreak
其次,我们将使用替换的结果重建原始 html 结构(通过重新添加<span id="scraped-portion">
)作为 X-Ray 调用的第一个参数。
希望你会喜欢它!
var x = Xray({
filters: {
replaceLineBreak: function (value) { return value.replace(/\<br\>/g, ';'); },
}
});
var html =
`
<span id="scraped-portion"><span class="bold">NodeJS</span><br>
<span class="bold">Version:</span> 8<br><span class="bold">Date released:</span> 2017 Jan<br><span class="bold">Description:</span>Some other text
</span>
`;
x(html,
'#scraped-portion@html | replaceLineBreak' /// Filter function called to replace '<br>' to ';'
)(function (err, obj) {
x(`<span id="scraped-portion">${obj}</span>`, /// Restore oroginal html structure to have the outer span with id 'scraped-portion
'#scraped-portion'
)(function (err2, obj2) { res.header("Content-Type", "text/html; charset=utf-8"); res.write(obj2); res.end(); })
});
产生以下字符串:
NodeJS; Version: 8;Date released: 2017 Jan;Description:Some other text
原始解决方案
<br>
为什么不在X-Ray 处理 html 代码之前替换所有出现的标签?
function tst(req, res) {
var x = Xray();
var html =
`
<span id="scraped-portion"><span class="bold">NodeJS</span><br>
<span class="bold">Version:</span> 8<br><span class="bold">Date released:</span> 2017 Jan<br><span class="bold">Description:</span>Some other text
</span>
`.replace(/\<br\>/g, ';');
x
(
html,
['span#scraped-portion']
)(function (err, obj) { res.header("Content-Type", "text/html; charset=utf-8"); res.write(JSON.stringify(obj, null, 4)); res.end(); })
;
}
然后你的代码会产生这样的结果
NodeJS;\n Version: 8;Date released: 2017 Jan;Description:Some other text\n
几乎可以满足您的要求