JavaScript去除字符串空格throwswitchcasebreakdefault
Posted web半晨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript去除字符串空格throwswitchcasebreakdefault相关的知识,希望对你有一定的参考价值。
function removeSpaces(paramsString, type)
paramsString = String(paramsString);
type = Number(type);
if (!paramsString || !type) throw new Error('参数有误');
switch (type)
case 1:
// 去除字符串所有的空格
paramsString = paramsString.replace(/\\s*/g, "");
break;
case 2:
// 去除字符串两头的空格
paramsString = paramsString.replace(/^\\s*|\\s*$/g, "");
break;
case 3:
// 去除字符串左侧的空格
paramsString = paramsString.replace(/^\\s*/, "");
break;
case 4:
// 去除字符串右侧的空格
paramsString = paramsString.replace(/(\\s*$)/g, "");
break;
case 5:
// 去除字符串两头的空格
paramsString = paramsString.trim();
break;
case 6:
// 去除字符串左侧的空格
paramsString = paramsString.trimLeft();
break;
case 7:
// 去除字符串右侧的空格
paramsString = paramsString.trimRight();
break;
default:
throw new Error('出错啦');
break;
return paramsString;
console.log(removeSpaces(' Hello World ', 1));
console.log(removeSpaces(' Hello World ', 2));
console.log(removeSpaces(' Hello World ', 3));
console.log(removeSpaces(' Hello World ', 4));
console.log(removeSpaces(' Hello World ', 5));
console.log(removeSpaces(' Hello World ', 6));
console.log(removeSpaces(' Hello World ', 7));
console.log(removeSpaces(' Hello World ', 8));
以上是关于JavaScript去除字符串空格throwswitchcasebreakdefault的主要内容,如果未能解决你的问题,请参考以下文章