zoukankan      html  css  js  c++  java
  • Js 常用函数

    //字符串是否包含指定字符串
    String.prototype.Contains = function (str, symbol) {
    if (symbol) {
    var arrysb = this.toString().split(symbol);
    return arrysb.contains(str);
    } else {
    var x = this.indexOf(str) >= 0;
    return x;
    }
    }


    //数组包含元素
    Array.prototype.contains = function (element) {

    for (var i = 0; i < this.length; i++) {
    if (this[i] == element) {
    return true;
    }
    }
    return false;
    }

    Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
    if (this[i] == val) {
    return i;
    }
    }
    return -1;
    };
    Array.prototype.removevalue = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
    this.splice(index, 1);
    }
    };

    //获得地址栏参数
    String.prototype.getRequest = function (name) {
    var svalue = this.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
    return svalue ? svalue[1] : svalue;
    }

    //格式化日期格式
    Date.prototype.Format = function (format) {
    var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(), //day
    "h+": this.getHours(), //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
    }
    return format;
    }

    //过滤特殊字符
    function stripscript(s) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()&mdash;—|{}【】‘;:”“'。,、?]");
    var rs = "";
    for (var i = 0; i < s.length; i++) {
    rs = rs + s.substr(i, 1).replace(pattern, '');
    }
    return rs;
    }

    //验证数字输入(允许输入小数点)
    function CheckNum(obj) {
    // 值允许输入一个小数点和数字
    obj.value = obj.value.replace(/[^d.]/g, ""); //先把非数字的都替换掉,除了数字和.
    obj.value = obj.value.replace(/^./g, ""); //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/.{2,}/g, "."); //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(".", "$#$").replace(/./g, "").replace("$#$", "."); //保证.只出现一次,而不能出现两次以上
    }

  • 相关阅读:
    【leetcode】二叉搜索树中的众数
    【leetcode】和为零的N个唯一整数
    【leetcode】统计位数为偶数的数字
    【leetcode】寻找数组的中心索引
    【leetcode】找出给定方程的正整数解
    【leetcode】寻找比目标字母大的最小字母
    【leetcode】找出井字棋的获胜者
    输出0~N之间的素数
    判断正整数是否是素数(数据结构课后练习题2.13a)
    052-85
  • 原文地址:https://www.cnblogs.com/linsu/p/4495025.html
Copyright © 2011-2022 走看看