zoukankan      html  css  js  c++  java
  • js的String.prototype

    (^\s+)|(\s+$)/g
    / pattern /g
    是js正则字符串的语法

    g (全文查找出现的所有 pattern)
    i (忽略大小写)
    m (多行查找)

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


    //批量替换,比如:str.ReplaceAll([/a/g,/b/g,/c/g],["aaa","bbb","ccc"])     

    String.prototype.ReplaceAll=function (A,B) {     

       
    var C=this;     

       
    for(var i=0;i<A.length;i++) {     

            C
    =C.replace(A[i],B[i]);     

        };     

       
    return C;     

    };     

    // 返回字符的长度,一个中文算2个
    String.prototype.ChineseLength=function()
    {
        return this.replace(/[^\x00-\xff]/g,"**").length;
    }
    // 判断字符串是否以指定的字符串结束
    String.prototype.EndsWith = function(str)
    {
        return this.substr(this.length - str.length) == str;
    }
    // 去掉字符左端的的空白字符
    String.prototype.LeftTrim = function()
    {
        return this.replace(/(^[\\s]*)/g, "");
    }
    // 去掉字符右端的空白字符
    String.prototype.RightTrim = function()
    {
        return this.replace(/([\\s]*$)/g, "");
    }
    // 判断字符串是否以指定的字符串开始
    String.prototype.StartsWith = function(str)
    {
        return this.substr(0, str.length) == str;
    }
    // 去掉字符两端的空白字符
    String.prototype.Trim = function()
    {
        return this.replace(/(^\s*)|(\s*$)/g, "");
    }

  • 相关阅读:
    android 手电筒的实现
    微信百度天气查询
    ExtJs自学教程(1):一切从API開始
    Adobe/Flash Media Server 5.0 linux 64位系统下的安装
    cocos2d-x 3.0 画图节点——Node
    codeforce 437B The Child and Set
    intel集成显卡linux驱动安装
    Linux下查看进程(程序)启动时的环境变量
    Linux设置和查看环境变量的方法
    兼容软件,Wine安装,Crossover安装,
  • 原文地址:https://www.cnblogs.com/zwbgreat/p/2862720.html
Copyright © 2011-2022 走看看