http://www.web521.com/web/577668/T660534.shtml

解决方案 »

  1.   

    <script language=javascript>
    var str="[SIZE=5]字号的大小[/SIZE]"
    var str2="字体颜色"
    re=/\[(.+?)=(.+?)\](.*?)\[\/\1\]/g
    str=str.replace(re,"<font style=\"$1:$2\">$3<\/font>")
    alert(str)
    str2=str2.replace(re,"<font style=\"$1:$2\">$3<\/font>")
    alert(str2)
    </script>
      

  2.   

    再加50分,上面这位大虾,能不能给一个java的写法,$1:$2这样,在java里好像不适用啊
      

  3.   

    引的包都是正则的包
    Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]"); 
    Matcher m = p.matcher("[SIZE=5]字号的大小[/SIZE]");
    if(m.matches()){
    System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
    }
    m = p.matcher("字体颜色");
    if(m.matches()){
    System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
    }
      

  4.   

    这个SIZE和COLOR不是固定值啊,这样写不行吧,也有可能是size=7,8,9等等,我都写进去也不行啊?
      

  5.   

    再加50,呵呵,我着急;
           Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]");
           Matcher m = p.matcher("[SIZE=7]字体颜色[/SIZE]");
           if(m.matches()){
           System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
           }这句运行不正确啊
      

  6.   

    可以啊,不过这个要具体情况具体加的,举个反例,再向上加条件
    Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]"); 
    Matcher m = p.matcher("[SIZE=7]字号的大小[/SIZE]");
    if(m.matches()){
    System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
    }
    }
      

  7.   

    大体上这样吧,具体分支你在改改
    String str="[SIZE=7][SIZE=5]字体颜色[/SIZE][/SIZE]";
    Pattern p = Pattern.compile("\\[(.+?)=([^\\]]+?)\\]([^\\[]+)(\\[(.+?)\\])+");
           Matcher m = p.matcher(str);
           String innerHTML=null;
           String innerText=null;
           if(m.matches()){
            innerHTML=m.replaceAll("\"$1:$2;\"");
            innerHTML=innerHTML.replaceAll("\\]\\[",";");
            p=Pattern.compile("(\\[[^\\]]+\\])+([^\\[]+)(\\[([^\\]]+?)\\])+");
            m=p.matcher(str);
            if(m.matches()){
            innerText=m.replaceAll("$2");
            }
            System.out.println("<font style="+innerHTML+">"+innerText+"</font>");
           }