zoukankan      html  css  js  c++  java
  • 去除空格的js 和 使用正则表达式替换

    去除空格的js 和 使用正则表达式替换  

    2010-09-15 15:11:21|  分类: 默认分类|字号 订阅

     
     
    1.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, "");
    }

    2.全文匹配替换
    var regExp = new RegExp("需要被替换的字符',"g")
    var text = "…………";
    text = text.replace(regExp , "替换的字符"); 

    3.方案
    String .prototype.trim = function(){
       var matches = this.match(/^[ \t\n\r]+/);
       var prefixLength = (matches == null) ? 0:matches[0].length;
       matches = this.match(/[ \t\r\n]+$/);
       var suffixLength = (matches == null) ? 0:matches[0].length;
       return this.slice(prefixLength,this.length-suffixLength);
    }


  • 相关阅读:
    文件处理--文件操作
    三元运算
    alex 推荐的书
    python字符串、列表和字典的说明
    运算符
    while else语句
    格式化输出
    数据类型-元组
    数据类型-集合
    字符串
  • 原文地址:https://www.cnblogs.com/guke/p/2751595.html
Copyright © 2011-2022 走看看