Skip to content

Instantly share code, notes, and snippets.

@KiligFei
Created June 3, 2021 03:12
Show Gist options
  • Save KiligFei/d2ba12c8fe6b12e7a65a6fb840bf781b to your computer and use it in GitHub Desktop.
Save KiligFei/d2ba12c8fe6b12e7a65a6fb840bf781b to your computer and use it in GitHub Desktop.
[去除空格] # JavaScript
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