js中去掉字符串的空格、回车换行

//例如下面这个json串,中间的\n表示换行

var str = "{'   retmsg':'success   ',\n'  trans_date':'   20170906'}";
console.log(str);
//"{'   retmsg':'success   ',
//'  trans_date':'   20170906'}"


//去掉空格
str = str.replace(/\ +/g,"");   
console.log(str);
//"{'retmsg':'success',
//'trans_date':'20170906'}"


//去掉回车换行        
str = str.replace(/[\r\n]/g,"");        
console.log(str);
//"{'retmsg':'success','trans_date':'20170906'}"

 

posted @ 2018-05-07 15:06  一江西流  阅读(3109)  评论(0编辑  收藏  举报