日期:2014-05-17  浏览次数:20674 次

正则表达式 高手来解
下面一段字符串

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";




../../这个地方我还要写成变量 可以自己设置为任意值 因为目录层次要变



------解决方案--------------------
Java code
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);

------解决方案--------------------
上面的漏了双引号了
不要动态的,那就直接写
Java code
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);