格式化excle表格複製的一列手機號,去重,並用逗號隔開

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>測試</title>
</head>
<style type="text/css">
*{
    margin:0;padding:0;
}
.wrap{
    width: 1000px;
    min-height: 500px;
    margin: 30px auto;
}
#phones{
    width:1000px;
    height:400px;
    outline: none;
}
#reset{
    width:80px;
    height: 40px;
    line-height: 40px;
    background:#0099ff;
    color:#fff;
    text-align:center;
    border-radius:4px;
    margin: 0 auto;
    cursor: pointer;
}
</style>
<body>
  <div class='wrap'>
      <textarea id='phones'></textarea>
    <div id='reset'>格式化</div>
  </div>
</body>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script>
 $('#reset').on('click',function(){
     var str = $.trim($('#phones').val())
     s2 =str.replace(/[\r\n\s+\u4e00-\u9fa5\a-zA-Z]/g,","); //去掉空格、回車換行、漢子、字母  
     console.log(s2);
     // 去重
     $('#phones').val(s2);
     var arr=s2.split(',');
    var newArr=[];
    var obj={};
    arr.forEach(function(item){
      if(obj[item]===undefined){
        obj[item]=1;
        newArr.push(item);
      }
    })
    $('#phones').val(newArr.toString()+',')
 })
</script>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章