<textarea contenteditable="true" id="t" onkeyup="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"), " ");
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>