JavaScript获取有效字符串

需求:有字符串,‘【空格】【空格】【空格】xxx【空格】yyy【空格】【空格】’
           得到有效字符串 xxx【空格】yyy

解决:使用正则表达式解决   

    function(str){

      return str.replace(/(^\s*)|(\s*$)/g, "");

    }

另:1、得到字符串xxx【空格】yyy【空格】【空格】
       解:

     function(str){

      return str.replace(/(^\s*)/g, "");

     }

   2、得到字符串【空格】【空格】【空格】xxx【空格】yyy

        解:

     function(str){

      return str.replace(/(\s*$)/g, "");

     }

参考文章:https://www.jb51.net/article/80070.htm

猜你喜欢

转载自blog.csdn.net/weixin_40538702/article/details/109069733