javascript匹配正则

正则表达式一直是自己的弱项,最近有机会恶补一下,论坛最近有人发帖,求123abc,2011-09-08 11:22;456abc,2011-09-08 11:22;8&%$#,2011-09-08 11:22;una89,2011-09-08 11:22;.....(很多很多...)
    输出:123abc|456abc|&%$#|una89
看到第一眼,我觉得用split,replace做比较方便,但感觉有点浪费内存,因为总感觉不断的截取,存放,操作比较平凡,但后来发现使用正则也很方便,于是就试了试
  <script type="text/javascript">
  <!--
	var str = "123abc,2011-09-08 11:22;456abc,2011-09-08 11:22;8&%$#,2011-09-08 11:22;una89,2011-09-08 11:22;";
	var rex = /([^,]*),([^;]*);/g,txt ,rs = [];
	
	while(txt = rex.exec(str) != null){
		rs.push(txt[1]);
	}
	alert(rs.join("|"));
  //-->
  </script>

你可能感兴趣的:(JavaScript)