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,"','");
    }

  

原文地址:https://www.cnblogs.com/cm1236/p/5607825.html