js字符串过滤实用代码

1.每隔4位数字后加","号。

<input type='text' id="test" placeholder="输入数字每隔4位后加,号">

  

window.onload=function(){
  document.getElementById("test").onkeyup=function(){
      var regex=new RegExp(/\d/);
          if(!regex.test(this.value)){
           this.style.border="1px dashed red";
          }else{
            this.style.border="1px solid black";
            this.value=this.value.replace(/(\d{4})(?=\d)/,"$1,");
        }  
    }  
}            

2.将空格替换为‘,’和‘“,”’

    var showT=document.getElementById('showT');
    function addcomma(){
        var textAreaV=document.getElementById('textArea').value;
        showT.innerHTML=textAreaV.replace(/\s/g,",");
    }
    function addquo(){
        var textAreaV=document.getElementById('textArea').value;
        showT.innerHTML=textAreaV.replace(/\s/g,"','");
    }

  

posted on 2016-06-22 17:04  过路的妖怪  阅读(1324)  评论(0编辑  收藏  举报