/* * 去掉字符串开头空白符 * */ function removeBeginBlank(str) { return str.replace(/^\s*/g,""); /* 返回替换后的字符串 */ } var str = " hahaha"; /* 要操作的字符串 */ removeBeginBlank(str); /* * 去掉字符串开头空白符结束 * */
/* * 去掉字符串尾部空白字符 * */ function removeTrailBlank(str) { return str.replace(/\s*$/g,""); /* 返回操作得到的字符串 */ } var str = "hahah "; /* 要操作的字符串 */ removeTrailBlank(str); /* * 去掉字符串尾部空白字符 * */
/* * 去掉两端空白字符 * */ function remove2PortBlank(str) { return str.replace(/^\s*|\s$/g,""); /* 返回操作得到的字符串 */ } var str = "hahah "; /* 要操作的字符串 */ removeTrailBlank(str); /* * 去掉两端空白字符 * */
/* * 去掉字符串所有空白符 * */ function removeAllBlank(str) { return str.replace(/\s*/g,""); /* 返回操作得到的字符串 */ } var str = " hah aha "; /* 要操作的字符串 */ removeAllBlank(str); /* * 去掉字符串所有空白符结束 * */
The above is the detailed content of How to remove whitespace characters on the left side, right side, whitespace characters on both sides, and all whitespace characters in a string. For more information, please follow other related articles on the PHP Chinese website!