我发现同样的内容,在JAVA和JS里面用正则replace后得到的结果不一样 String java = str.replaceAll("\\<[^>]+\\>", "").length(); String javascript = str.replace(/\<[^>]+\>/g,'').length();我发现同样的内容,在java和js里面用正则replace后得到的结果不一样!导致前台check成功,后台却失败!不知道是我写错了还是本来就有这样的问题啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你要实现一个什么样的功能呢?要在前后台都replace吗?最好做一个统一。到时候改得时候也好改。 功能是能是去掉<>中间,的全部内容测试内容如下<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啊! 参考:http://doc.linuxpk.com/44575.html String java = str.replaceAll("\\ <[^>]+\\>", "").length(); String javascript = str.replace(/\ <[^>]+\>/g,'').length(); 楼主你的两个语句全部都是错误的!!首先他们返回的值应该是整形吗?JAvascript中没有String类型!!! Integer len = str.replaceAll("\\<[^>]+\\>", "").length(); var len = str.replace(/\<[^>]+\>/g,'').length; 不考虑类型,js的string只有length属性没有length()方法的,楼主代码居然能跑起来喵? 不好意思写的着急了写错误了...........重新整理如下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.测试结果发现两个结果竟然不一样!正则都是一样的 以前遇到过类似的问题,后来发现js里的replace函数和想象的不一样var str = "ababab";alert(str.replace("a","c"));结果是cbabab,而不是cbcbcb 1."str",又是楼主手误吧2.当心空格,楼主你java的正则里有一个空格,而js的没有.除了这两点,我跑起来结果是一样的喵~~` js里的replace等同于java的replaceAll前面应该是正则而不是普通字符串,不然你会发现只替换了一次.想用java式的replace就自己用循环写一个吧喵~~` 你qq,msn多少啊?我跑起来为什么不一样啊? 可以把两个的结果分别贴出来吗?我是说去掉java里的那个空格之后的,刚才我没详细比对结果,只看到没有<...>了喵~`` java的正则里有一个空格,而js的没有. 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一样的啊…… 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一样的啊…… 你是不是写错了啊?< > 前面加上 \ 或 \\ 这些做什么啊? 3.javascript代码 var len = str.replace(/\<[^>]+\>/g,'').length; 4.java代码 Integer len = "str".replaceAll("\\<[^>]+\\>", "").length(); js都要用< > 前面加上 \ 或 \\ 啊! HttpURLConnection如何添加请求头? 我在用输入输出流的时候碰到一个问题 VB翻译成JAVA, 各位大虾帮忙看看问题在哪 为什么老提示我 缺少class 或者interface 怎样在调试时可以看到jdk类中的方法中的局部变量的值? 使用jni调用c++编译的dll为什么不是立即执行? 请问怎样用JDBC访问My SQL数据库啊? 关于WebSphere配置的几个问题 主要问题:在一个Frame中有个菜单,点击菜单选项后,在Frame中画图,请问如何实现? 关于java的动态代理 一个很简单的socket通信问题,请高手看一下 请教jawin的参数问题
测试内容如下<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啊!
http://doc.linuxpk.com/44575.html
String javascript = str.replace(/\ <[^>]+\>/g,'').length();
楼主你的两个语句全部都是错误的!!首先他们返回的值应该是整形吗?JAvascript中没有String类型!!!
Integer len = str.replaceAll("\\<[^>]+\\>", "").length();
var len = str.replace(/\<[^>]+\>/g,'').length; 不考虑类型,js的string只有length属性没有length()方法的,楼主代码居然能跑起来喵?
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.测试结果
发现两个结果竟然不一样!正则都是一样的
alert(str.replace("a","c"));结果是cbabab,而不是cbcbcb
2.当心空格,楼主你java的正则里有一个空格,而js的没有.除了这两点,我跑起来结果是一样的喵~~`
想用java式的replace就自己用循环写一个吧喵~~`
我是说去掉java里的那个空格之后的,
刚才我没详细比对结果,只看到没有<...>了喵~``
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
一样的啊……
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
一样的啊……
var len = str.replace(/\<[^>]+\>/g,'').length; 4.java代码
Integer len = "str".replaceAll("\\<[^>]+\\>", "").length(); js都要用
< > 前面加上 \ 或 \\ 啊!