You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var str = ' abc d e f g ';
function trim(str) {
var reg = /\s+/g;
if (typeof str === 'string') {
var trimStr = str.replace(reg,'');
}
console.log(trimStr)
}
trim(str)
dream-one, lianghuahua, Zhengqbbb, PengYYYYY, mike1026915 and 27 morewangdabaoqq, SoberK, blueRoach, irockyan, cl-1234 and 6 morexianchenxy and Rednaxela-2
conststr=' s t r 'constPOSITION=Object.freeze({left: Symbol(),right: Symbol(),both: Symbol(),center: Symbol(),all: Symbol(),})functiontrim(str,position=POSITION.both){if(!!POSITION[position])thrownewError('unexpected position value')switch(position){case(POSITION.left):
str=str.replace(/^\s+/,'')break;case(POSITION.right):
str=str.replace(/\s+$/,'')break;case(POSITION.both):
str=str.replace(/^\s+/,'').replace(/\s+$/,'')break;case(POSITION.center):
while(str.match(/\w\s+\w/)){str=str.replace(/(\w)(\s+)(\w)/,`$1$3`)}break;case(POSITION.all):
str=str.replace(/\s/g,'')break;default:
}returnstr}constresult=trim(str)console.log(`|${result}|`)// |s t r|
kangw3n, persist-xyz, pma934, xiaoxiaocoder, Goo24 and 37 mores33you
jiamianmao and mocha-optsMarszed, funlee, Starscape000, MartinGao, Jaygk and 18 morechengazhen, ShawnTseng, fahcode, v1kt0r820, quuer and 4 morevizoy and LillianLi110130
Activity
haizhilin2013 commentedon Apr 17, 2019
欢迎大家展开多种不同的方法
yxkhaha commentedon Apr 18, 2019
qq674785876 commentedon May 5, 2019
var trim = function(str){
return str.replace(/\s*/g,"");
}
str.replace(/\s*/g,""); //去除字符串内所有的空格
str.replace(/^\s*|\s*$/g,""); //去除字符串内两头的空格
str.replace(/^\s*/,""); //去除字符串内左侧的空格
str.replace(/(\s*$)/g,""); //去除字符串内右侧的空格
shulandmimi commentedon May 7, 2019
linghucq1 commentedon May 8, 2019
qqdnnd commentedon May 16, 2019
tzjoke commentedon May 16, 2019
string.replace(/\s/g, '')
string.split(' ').join('')
yangchunboy commentedon May 27, 2019
这样不是很简单吗 @haizhilin2013
Hxiaongrong commentedon May 28, 2019
function noSpace(str){
if (!str) return ''
return str.split('').filter(item => item !== ' ').join('')
}
Zhou-Bill commentedon Jun 4, 2019
" 123 56 ".replace(/\s+/g, "")
likeke1997 commentedon Jun 5, 2019
myprelude commentedon Jun 13, 2019
wanghaooo commentedon Jun 17, 2019
const trim = (str) => str.split('').filter((item) => item !== ' ').join('');
Damon99999 commentedon Jun 17, 2019
str.replace(/[ ]/g, "");
160 remaining items