<script>
var strr = " 1 ad dertasdf sdfASDFDF DFG SDFG "
// type 1-所有空格,2-前后空格,3-前空格,4-后空格
function trim(str, type) {
switch (type) {
case 1: return str.replace(/\s+/g, "");
case 2: return str.replace(/(^\s*)|(\s*$)/g, "");
case 3: return str.replace(/(^\s*)/g, "");
case 4: return str.replace(/(\s*$)/g, "");
default: return str;
}
}
console.log(trim(strr, 1)) // "1addertasdfsdfASDFDFDFGSDFG"
</script>
js去空格正则
最新推荐文章于 2024-08-03 05:56:08 发布