js 批量空格替換成

<textarea contenteditable="true" id="t" οnkeyup="this.value=this.value.replace(/\s+/g,'')" rows="3" placeholder="請填寫詳細地址,不超過100個字"></textarea>

<script>
    var d = document.getElementById( "t" );
    document.addEventListener( "keyup", function() {
        d.innerHTML = d.innerHTML.replace(/\s+/g,'');
    } );

var a = "hello  word word  word  word"; 
var b = a.replace(new RegExp(" ", "gm"), "&nbsp;"); 
console.log(b); 

//終極殺招
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { 
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) { 
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); 
    } else { 
        return this.replace(reallyDo, replaceWith); 
    } 
} 
</script>
 

 

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