Created
June 3, 2021 03:12
-
-
Save KiligFei/d2ba12c8fe6b12e7a65a6fb840bf781b to your computer and use it in GitHub Desktop.
[去除空格] # JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const trim = function (str, type) { | |
// 去除空格, type: 1-所有空格 2-前后空格 3-前空格 4-后空格 | |
type = type || 1; | |
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment