zoukankan      html  css  js  c++  java
  • js使用正则表达式去空格

    写成类的方法格式如下:(str.trim();)


      <script language="javascript">
       String.prototype.trim=function(){
          return this.replace(/(^s*)|(s*$)/g, "");
       }
       String.prototype.ltrim=function(){
          return this.replace(/(^s*)/g,"");
       }
       String.prototype.rtrim=function(){
          return this.replace(/(s*$)/g,"");
       }
      </script>
    写成函数可以这样:(trim(str))
      <script type="text/javascript">
       function trim(str){ //删除左右两端的空格
           return str.replace(/(^s*)|(s*$)/g, "");
       }
       function ltrim(str){ //删除左边的空格
           return str.replace(/(^s*)/g,"");
       }
       function rtrim(str){ //删除右边的空格
           return str.replace(/(s*$)/g,"");
       }    

      function trimStr(str){return str.replace(/(^s*)|(s*$)/g,"");}

      用的时候就是直接               var 变量=trimStr(需要去空格的字符串)


      </script>

  • 相关阅读:
    UML箭头含义整理
    协变返回类型
    Thymeleaf取出model中的数据
    宝塔面板中的mysql查看密码问题
    宝塔面板如何登录
    mysql查看数据库、表的基本语句
    springboot拦截器实例
    Thymeleaf中的fragments学习
    食物链
    银河英雄传说
  • 原文地址:https://www.cnblogs.com/tancp/p/3831057.html
Copyright © 2011-2022 走看看