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

  • 相关阅读:
    Longest Mountain in Array 数组中的最长山脉
    css 解决 图片 底部 3像素问题
    获取当前年月日2020-09-30格式
    vue + elememt ui table 实现滚屏效果
    滚动字
    layui 之监听 select 的变化
    正则匹配非汉字
    layui form里的select元素动态赋值无效
    layui 之 弹框重新打开 upload无效
    GPS坐标转百度坐标
  • 原文地址:https://www.cnblogs.com/zwbgreat/p/2862720.html
Copyright © 2011-2022 走看看