Java 正则表达式替换另一个表达式中的所有表达式
我需要将所有Java 正则表达式替换另一个表达式中的所有表达式,java,regex,Java,Regex,我需要将所有和标记替换为“”仅在标记内 <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");<
和
标记替换为“”仅在
标记内
<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>
...
println(“我需要你的帮助”);
println(“因为我是新手”);
println(“我知道”);
println(“你可以帮我”);
大胆呆在这里
....
我想:
....
println(“我需要你的帮助”);
println(“因为我是新手”);
println(“我知道”);
println(“你可以帮我”);
大胆呆在这里
....
如何使用replaceAll()完成此操作?这是一个棘手的问题,因为您不能在一个组中收集多个项目,同时重复该组()。为了解决这个问题,我通过使用“lookaround”modifiiers()改变解析器查看它的方式
var text=`
println(“我需要你的帮助”);
println(“因为我是新手”);
println(“我知道”);
println(“你可以帮我”);
大胆呆在这里
`;
VAR Re=(())([s\s] *?)()考虑使用HTML解析器而不是正则表达式。过去我使用过JTHOP,但是我肯定还有其他的方法可以帮助解决这个问题。为什么不投票?因为我不能预测这个问题以前被问过了吗?我能理解这个问题的下落,但是答案,BTW,工作得很好。荒谬的。
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);