正则表达式 高手来解
下面一段字符串
window.parent.getNodeCodeByUrl("as/as/s.json");
我要将它替换成window.location.href="../../as/as/s.html";
如果是
window.parent.getNodeCodeByUrl("ac/bc/as/b.json");
替换成window.location.href="../../ac/bc/as/s.html";
或
window.location.href="../../../ac/bc/as/s.html";
../../这个地方我还要写成变量 可以自己设置为任意值 因为目录层次要变
[解决办法]
String prefix="../../"; String source="window.parent.getNodeCodeByUrl(\"as/as/s.json\")" ; String result=source.replaceAll("window\\.parent\\.getNodeCodeByUrl\\(\"(.*?)\\.json\"\\)","window.location.href=\""+prefix+"$1.html\""); System.out.println(result);
[解决办法]
上面的漏了双引号了
不要动态的,那就直接写
String str = "window.parent.getNodeCodeByUrl(\"ac/bc/as/b.json\");";str = str.replaceAll("(?i)window[.]parent[.]getNodeCodeByUrl[(]\"(.*?)/(.*?)[.]json\"[)].*", "windon.location.href=\"../../../$1/page/$2.html\";");System.out.println(str);