<!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>