String java       = str.replaceAll("\\<[^>]+\\>", "").length();
String javascript = str.replace(/\<[^>]+\>/g,'').length();
我发现同样的内容,在java和js里面用正则replace后得到的结果不一样!导致前台check成功,后台却失败!
不知道是我写错了还是本来就有这样的问题啊?

解决方案 »

  1.   

    你要实现一个什么样的功能呢?要在前后台都replace吗?最好做一个统一。到时候改得时候也好改。
      

  2.   

    功能是能是去掉<>中间,的全部内容
    测试内容如下<div>
    <ul>
        <li><a href="http://news.sina.com.cn/s/2008-11-18/014316671435.shtml" target="_blank"><font color="#000099">彩票店主归还彩民10万奖金</font></a> <a href="http://news.sina.com.cn/s/2008-11-18/031516672046.shtml" target="_blank"><font color="#000099">男孩下身赤裸跪街头三日</font></a></li>
        <li><a class="videoNewsLeft" href="http://video.sina.com.cn/news/" target="_blank"><font color="#000099">男子劫持亲生儿将其砍瞎</font></a> <a href="http://news.sina.com.cn/s/2008-11-18/010616671232.shtml" target="_blank"><font color="#000099">同性恋者遭暗恋对象劫杀</font></a></li>
        <li><a href="http://news.sina.com.cn/s/2008-11-18/052616672844.shtml" target="_blank"><font color="#000099">判决书将人工流产写成工人流产</font></a> <a href="http://news.sina.com.cn/s/2008-11-18/040916672445.shtml" target="_blank"><font color="#000099">职高女生在宿舍临盆</font></a></li>
        <li><a href="http://news.sina.com.cn/s/2008-11-18/033216671946.shtml" target="_blank"><font color="#000099">副镇长涉嫌受贿挪用公款三千万</font></a> <a href="http://news.sina.com.cn/s/2008-11-18/065016673663.shtml" target="_blank"><font color="#000099">服刑医生照领工资</font></a></li>
    </ul>
    </div>
    你要实现一个什么样的功能呢?要在前后台都replace吗?最好做一个统一。到时候改得时候也好改。没办法统一啊...............前台用js check啊后台用java check啊!
      

  3.   

    参考:
    http://doc.linuxpk.com/44575.html
      

  4.   

    String java      = str.replaceAll("\\ <[^>]+\\>", "").length(); 
    String javascript = str.replace(/\ <[^>]+\>/g,'').length(); 
    楼主你的两个语句全部都是错误的!!首先他们返回的值应该是整形吗?JAvascript中没有String类型!!!
      

  5.   


    Integer len = str.replaceAll("\\<[^>]+\\>", "").length(); 
    var len = str.replace(/\<[^>]+\>/g,'').length; 不考虑类型,js的string只有length属性没有length()方法的,楼主代码居然能跑起来喵?
      

  6.   

    不好意思写的着急了写错误了...........重新整理如下
    1.功能:功能是去掉 <>中间,的全部内容 
    2.测试:测试内容如下 <div> 
    <ul> 
        <li> <a href="http://news.sina.com.cn/s/2008-11-18/014316671435.shtml" target="_blank"> <font color="#000099">彩票店主归还彩民10万奖金 </font> </a> <a href="http://news.sina.com.cn/s/2008-11-18/031516672046.shtml" target="_blank"> <font color="#000099">男孩下身赤裸跪街头三日 </font> </a> </li> 
        <li> <a class="videoNewsLeft" href="http://video.sina.com.cn/news/" target="_blank"> <font color="#000099">男子劫持亲生儿将其砍瞎 </font> </a> <a href="http://news.sina.com.cn/s/2008-11-18/010616671232.shtml" target="_blank"> <font color="#000099">同性恋者遭暗恋对象劫杀 </font> </a> </li> 
        <li> <a href="http://news.sina.com.cn/s/2008-11-18/052616672844.shtml" target="_blank"> <font color="#000099">判决书将人工流产写成工人流产 </font> </a> <a href="http://news.sina.com.cn/s/2008-11-18/040916672445.shtml" target="_blank"> <font color="#000099">职高女生在宿舍临盆 </font> </a> </li> 
        <li> <a href="http://news.sina.com.cn/s/2008-11-18/033216671946.shtml" target="_blank"> <font color="#000099">副镇长涉嫌受贿挪用公款三千万 </font> </a> <a href="http://news.sina.com.cn/s/2008-11-18/065016673663.shtml" target="_blank"> <font color="#000099">服刑医生照领工资 </font> </a> </li> 
    </ul> 
    </div> 
    3.javascript代码
    var len = str.replace(/\<[^>]+\>/g,'').length; 4.java代码
    Integer len = "str".replaceAll("\\<[^>]+\\>", "").length(); 
    5.测试结果
    发现两个结果竟然不一样!正则都是一样的
      

  7.   

    以前遇到过类似的问题,后来发现js里的replace函数和想象的不一样var str = "ababab";
    alert(str.replace("a","c"));结果是cbabab,而不是cbcbcb
      

  8.   

    1."str",又是楼主手误吧
    2.当心空格,楼主你java的正则里有一个空格,而js的没有.除了这两点,我跑起来结果是一样的喵~~`
      

  9.   

    js里的replace等同于java的replaceAll前面应该是正则而不是普通字符串,不然你会发现只替换了一次.
    想用java式的replace就自己用循环写一个吧喵~~`
      

  10.   

    你qq,msn多少啊?我跑起来为什么不一样啊?
      

  11.   

    可以把两个的结果分别贴出来吗?
    我是说去掉java里的那个空格之后的,
    刚才我没详细比对结果,只看到没有<...>了喵~``
      

  12.   

    java的正则里有一个空格,而js的没有. 
      

  13.   


    public static void main(String[] args) {

    String tar = "<div>"+
    "<ul>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/014316671435.shtml\" target=\"_blank\"> <font color=\"#000099\">彩票店主归还彩民10万奖金 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/031516672046.shtml\" target=\"_blank\"> <font color=\"#000099\">男孩下身赤裸跪街头三日 </font> </a> </li>"+
    "    <li> <a class=\"videoNewsLeft\" href=\"http://video.sina.com.cn/news/\" target=\"_blank\"> <font color=\"#000099\">男子劫持亲生儿将其砍瞎 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/010616671232.shtml\" target=\"_blank\"> <font color=\"#000099\">同性恋者遭暗恋对象劫杀 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/052616672844.shtml\" target=\"_blank\"> <font color=\"#000099\">判决书将人工流产写成工人流产 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/040916672445.shtml\" target=\"_blank\"> <font color=\"#000099\">职高女生在宿舍临盆 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/033216671946.shtml\" target=\"_blank\"> <font color=\"#000099\">副镇长涉嫌受贿挪用公款三千万 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/065016673663.shtml\" target=\"_blank\"> <font color=\"#000099\">服刑医生照领工资 </font> </a> </li>"+
    "</ul>"+
    "</div>";
    System.out.println(tar.replaceAll("\\ <[^>]+\\>", ""));
    System.out.println(tar.replaceAll("\\ <[^>]+\\>", "").length());

    }
    <div><ul>   彩票店主归还彩民10万奖金男孩下身赤裸跪街头三日   男子劫持亲生儿将其砍瞎同性恋者遭暗恋对象劫杀   判决书将人工流产写成工人流产职高女生在宿舍临盆   副镇长涉嫌受贿挪用公款三千万服刑医生照领工资</ul></div>
    123<html><script type="text/javascript"> function getStr(){
    var tar = "<div>"+
    "<ul>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/014316671435.shtml\" target=\"_blank\"> <font color=\"#000099\">彩票店主归还彩民10万奖金 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/031516672046.shtml\" target=\"_blank\"> <font color=\"#000099\">男孩下身赤裸跪街头三日 </font> </a> </li>"+
    "    <li> <a class=\"videoNewsLeft\" href=\"http://video.sina.com.cn/news/\" target=\"_blank\"> <font color=\"#000099\">男子劫持亲生儿将其砍瞎 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/010616671232.shtml\" target=\"_blank\"> <font color=\"#000099\">同性恋者遭暗恋对象劫杀 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/052616672844.shtml\" target=\"_blank\"> <font color=\"#000099\">判决书将人工流产写成工人流产 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/040916672445.shtml\" target=\"_blank\"> <font color=\"#000099\">职高女生在宿舍临盆 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/033216671946.shtml\" target=\"_blank\"> <font color=\"#000099\">副镇长涉嫌受贿挪用公款三千万 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/065016673663.shtml\" target=\"_blank\"> <font color=\"#000099\">服刑医生照领工资 </font> </a> </li>"+
    "</ul>"+
    "</div>";

    alert(tar.replace(/\ <[^>]+\>/g,'').length);
    alert(tar.replace(/\ <[^>]+\>/g,''));
    }
    </script>
    <body onload="getStr()"></body>
    </html>
    <div><ul>   彩票店主归还彩民10万奖金男孩下身赤裸跪街头三日   男子劫持亲生儿将其砍瞎同性恋者遭暗恋对象劫杀   判决书将人工流产写成工人流产职高女生在宿舍临盆   副镇长涉嫌受贿挪用公款三千万服刑医生照领工资</ul></div>
    123
    一样的啊……
      

  14.   


    public static void main(String[] args) {

    String tar = "<div>"+
    "<ul>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/014316671435.shtml\" target=\"_blank\"> <font color=\"#000099\">彩票店主归还彩民10万奖金 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/031516672046.shtml\" target=\"_blank\"> <font color=\"#000099\">男孩下身赤裸跪街头三日 </font> </a> </li>"+
    "    <li> <a class=\"videoNewsLeft\" href=\"http://video.sina.com.cn/news/\" target=\"_blank\"> <font color=\"#000099\">男子劫持亲生儿将其砍瞎 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/010616671232.shtml\" target=\"_blank\"> <font color=\"#000099\">同性恋者遭暗恋对象劫杀 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/052616672844.shtml\" target=\"_blank\"> <font color=\"#000099\">判决书将人工流产写成工人流产 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/040916672445.shtml\" target=\"_blank\"> <font color=\"#000099\">职高女生在宿舍临盆 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/033216671946.shtml\" target=\"_blank\"> <font color=\"#000099\">副镇长涉嫌受贿挪用公款三千万 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/065016673663.shtml\" target=\"_blank\"> <font color=\"#000099\">服刑医生照领工资 </font> </a> </li>"+
    "</ul>"+
    "</div>";
    System.out.println(tar.replaceAll("\\ <[^>]+\\>", ""));
    System.out.println(tar.replaceAll("\\ <[^>]+\\>", "").length());

    }
    <div><ul>   彩票店主归还彩民10万奖金男孩下身赤裸跪街头三日   男子劫持亲生儿将其砍瞎同性恋者遭暗恋对象劫杀   判决书将人工流产写成工人流产职高女生在宿舍临盆   副镇长涉嫌受贿挪用公款三千万服刑医生照领工资</ul></div>
    123<html><script type="text/javascript"> function getStr(){
    var tar = "<div>"+
    "<ul>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/014316671435.shtml\" target=\"_blank\"> <font color=\"#000099\">彩票店主归还彩民10万奖金 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/031516672046.shtml\" target=\"_blank\"> <font color=\"#000099\">男孩下身赤裸跪街头三日 </font> </a> </li>"+
    "    <li> <a class=\"videoNewsLeft\" href=\"http://video.sina.com.cn/news/\" target=\"_blank\"> <font color=\"#000099\">男子劫持亲生儿将其砍瞎 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/010616671232.shtml\" target=\"_blank\"> <font color=\"#000099\">同性恋者遭暗恋对象劫杀 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/052616672844.shtml\" target=\"_blank\"> <font color=\"#000099\">判决书将人工流产写成工人流产 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/040916672445.shtml\" target=\"_blank\"> <font color=\"#000099\">职高女生在宿舍临盆 </font> </a> </li>"+
    "    <li> <a href=\"http://news.sina.com.cn/s/2008-11-18/033216671946.shtml\" target=\"_blank\"> <font color=\"#000099\">副镇长涉嫌受贿挪用公款三千万 </font> </a> <a href=\"http://news.sina.com.cn/s/2008-11-18/065016673663.shtml\" target=\"_blank\"> <font color=\"#000099\">服刑医生照领工资 </font> </a> </li>"+
    "</ul>"+
    "</div>";

    alert(tar.replace(/\ <[^>]+\>/g,'').length);
    alert(tar.replace(/\ <[^>]+\>/g,''));
    }
    </script>
    <body onload="getStr()"></body>
    </html>
    <div><ul>   彩票店主归还彩民10万奖金男孩下身赤裸跪街头三日   男子劫持亲生儿将其砍瞎同性恋者遭暗恋对象劫杀   判决书将人工流产写成工人流产职高女生在宿舍临盆   副镇长涉嫌受贿挪用公款三千万服刑医生照领工资</ul></div>
    123
    一样的啊……
      

  15.   

    你是不是写错了啊?< > 前面加上 \ 或 \\ 这些做什么啊?
      

  16.   

    3.javascript代码 
    var len = str.replace(/\<[^>]+\>/g,'').length; 4.java代码 
    Integer len = "str".replaceAll("\\<[^>]+\\>", "").length(); js都要用
    < > 前面加上 \ 或 \\ 啊!