// 去除空格
String.prototype.Trim = function() {
return this.replace(/\s+/g, "");
}
例:
let originStr = " hello, how are you? "
let trimmedStr = originStr.trim() // 去除首尾空格,结果"hello, how are you?"
let resultStr = trimmedStr.replace(/\s+/g, ' ') // 去除多余空格,结果"hello, how are you?"
// 去除换行
function ClearBr(key) {
key = key.replace(/<\/?.+?>/g,"");
key = key.replace(/[\r\n]/g, ""); return
}
\s* 表示若干个空格(可以是0个)
\s+ 表示一个或多个空格