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

  • 相关阅读:
    JAVA程序员面试32问
    在做物流的库存管理系统里,需要注意。。。。。
    在写自动更新程序中出现的问题
    数据库设计中的五个范式
    cPickle / pickle
    python总结1
    python总结2
    汉明距离(Hamming distance)
    python中pickle的用法
    NET面试题
  • 原文地址:https://www.cnblogs.com/zwbgreat/p/2862720.html
Copyright © 2011-2022 走看看