zoukankan      html  css  js  c++  java
  • 正则简单操作cookie、url search

    正则操作cookie、url

    • getCookie
    function getCookie(key) {
        var cookies = window.document.cookie,
            reg = new RegExp('\S*' + key + '=[^;]*', 'gi');
        var result = cookies.match(reg);
        return result && result[0].substr(0, key.length) === key ? result[0].substr(key.length + 1) : false;
    }
    
    • setCookie
    function setCookie(key, value, expires) { // expires为天数
        var date = new Date();
        date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
        document.cookie = key + '=' + value + ';expires=' + date;
    }
    
    • getQuery
    function getQuery(key) {
        var search = window.location.search,
            reg = new RegExp('[^&]*' + key + '=[^&]*', 'gi');
        var result = search.substr(1).match(reg);
        return result && result[0].substr(0, key.length) === key ? window.decodeURIComponent(result[0].substr(key.length + 1)) : false;
    }
    
    • getUrl
    function getUrl() {
        var search = window.location.search,
            reg = /[^=]+/g;
        var deal = search.substr(1).split('&'),
            result = {};
        deal.forEach(function(item) {
            var temp = item.match(reg);
            temp && (result[temp[0]] = decodeURIComponent(temp[1]));
        });
        return result;
    }
    
  • 相关阅读:
    UVA
    [CQOI2018] 社交网络
    UVA
    51nod 1314 定位系统
    51nod 1211 数独
    51nod 1392 装盒子
    51nod1253 Kundu and Tree
    51nod1313 完美串
    51nod1039 x^3 mod p
    51nod1369 无穷印章
  • 原文地址:https://www.cnblogs.com/ljwk/p/9968003.html
Copyright © 2011-2022 走看看