我只需要在<b>
标记内将所有</b>
和<pre>
标记替换为“”。
我有:
<html>
...
<pre>
<b>println("I need your help");</b>
<b>println("because Iam newbie");</b>
</pre>
<pre>
<b>println("I know");</b>
<b>println("you can help me");</b>
</pre>
<b>bold stay here</b>
....
</html>
我想要:
<html>
....
<pre>
println("I need your help");
println("because Iam newbie");
</pre>
<pre>
println("I know");
println("you can help me");
</pre>
<b>bold stay here</b>
....
</html>
我该如何使用replaceAll()?
答案 0 :(得分:-1)
这很棘手,因为您不能在一个组中收集多个物品,并且同时重复该组(https://www.regular-expressions.info/captureall.html)。为了解决这个问题,我通过使用“环视”修饰符(https://www.regular-expressions.info/lookaround.html)来更改解析器的查看方式。
var text = `<html>
<pre>
<b>println("I need your help");</b>
<b>println("because Iam newbie");</b>
</pre>
<pre>
<b>println("I know");</b>
<b>println("you can help me");</b>
</pre>
<b>bold stay here</b>
</html>`;
var re = /((<b>)([\s\S]*?)(<\/b)>)(?<=(<pre>[\s\S]*?))(?=([\s\S]*?<\/pre>))/gm;
var mod = text.replace(re,function() {
// "arguments" is an array of all arguments
// passed (regardless of the function signature)
console.log(arguments);
return arguments[3];
});
console.log(text);
console.log(mod);