- 字符串中多个空格替换为一个
var sVal = 'this is a test';
/**
*结果为 'this is a test'
*/
sVal = sVal.replace(/\s+/g, ' ');
- 去除字符串前后空格
var sVal = ' hi man ';
/**
*结果为 'hi man'
*(^\s*)为去除左边,(\s*$)为去除右边
*/
sVal = sVal.replace(/(^\s*)|(\s*$)/g, '');
- 多个值转换为数组
/**
*连上前面的两个方法
*结果为 ['you', 'and', 'me']
*/
var sVal = ' you and me ';
sVal = sVal.replace(/\s+/g, ' ');
sVal = sVal.replace(/(^\s*)|(\s*$)/g, '');
var aVal = sVal.split(" ");