jQuery截取字符串、日期字符串转Date、获取html中的纯文本

jQuery截取字符串、日期字符串转Date、获取html中的纯文本。

var com = com || {};
(function ($, com) {
    /*
    * 截取字符串
    * @param str:要截取的字符串
    * @param len:保留多少字符
    * @param symbol:超过之后字符串末端要添加的字符
    */
    com.cutStr = function (str, len, symbol) {
        if (symbol == undefined) {
            symbol = "...";
        }
        len = len || 25;
        var result = str;
        if (str) {
            if (str.length && str.length > len)
                result = str.substr(0, len) + symbol;
        }
        return result;
    },
    /*
    * 将日期字符串转化为Date
    * (如:将"2016-12-24 20:13:14"转化为Date格式)
    * @param d:待转化字符串(传入的时间不能有毫秒)
    */
    com.getDate = function (d) {
        //部分浏览器(IE)不支持日期格式“yyyy-MM-dd hh:mm:ss”,必须将“-”转化为“/”
        var date = new Date(Date.parse(d.replace(/-/g, "/")));
        return date;
    },
    /*
    * 获取html代码的纯文本
    * @param html
    */
    com.removeHTMLTag = function (html) {
        html = html.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
        html = html.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
        //html = html.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
        html = html.replace(/&nbsp;/ig, '');//去掉&nbsp;
        html = html.replace(/\s/g, ''); //将空格去掉
        return html;
    }
})(jQuery, com);

 

posted @ 2017-01-11 11:51  Hi.wz  阅读(5702)  评论(0编辑  收藏  举报