1.去除所有空格: string.replace(/\s+/g,""); 2.去除两头空格: string.replace(/^\s+|\s+$/g,""); 3.去除左空格: string.replace( /^\s*/, ''); 4.去除右空格: string.replace(/(\s*$)/g, "");