使用js去除字符串内所带有空格,有以下三种方法:
( 1 ) replace正则匹配方法
去除字符串内所有的空格:str = str.replace(/s*/g,"");
去除字符串内两头的空格:str = str.replace(/^s*|s*$/g,"");
去除字符串内左侧的空格:str = str.replace(/^s*/,"");
去除字符串内右侧的空格:str = str.replace(/(s*$)/g,"");
示例:
var str = " 6 6 "; var str_1 = str.replace(/s*/g,""); console.log(str_1); //66
var str = " 6 6 "; var str_1 = str.replace(/^s*|s*$/g,""); console.log(str_1); //6 6//输出左右侧均无空格
var str = " 6 6 "; var str_1 = str.replace(/^s*/,""); console.log(str_1); //6 6 //输出右侧有空格左侧无空格
var str = " 6 6 "; var str_1 = str.replace(/(s*$)/g,""); console.log(str_1); // 6 6//输出左侧有空格右侧无空格