文章分类 Classification
js正则那些事
稿件来源: 阳光企业网站管理系统 撰稿作者: 太阳光 发表日期: 2013-06-26 阅读次数: 86 查看权限: 游客查看
记录一些正则
js中去除字符串两端的空格
String.prototype.trim=function() { return this.replace(/^\s*|\s*$/g, "")}
格式化HTML
String(text).replace(/&/g,'&').replace(/\</g,'<').replace(/\>/g,'>').replace(/"/g, '"').replace(/'/g, ''');
判断各浏览器
DOC = document, isStrict = DOC.compatMode == "CSS1Compat", isOpera = toString.call(window.opera) == "[object Opera]", isChrome = check(/chrome/), isWebKit = check(/webkit/), isSafari = !isChrome && check(/safari/), isSafari2 = isSafari && check(/applewebkit\/4/), isSafari3 = isSafari && check(/version\/3/), isSafari4 = isSafari && check(/version\/4/), isIE = !!window.attachEvent && !isOpera, isIE7 = isIE && check(/msie 7/), isIE8 = isIE && check(/msie 8/), isIE6 = isIE && !isIE7 && !isIE8, isGecko = !isWebKit && check(/gecko/), isGecko2 = isGecko && check(/rv:1\.8/), isGecko3 = isGecko && check(/rv:1\.9/), isBorderBox = isIE && !isStrict, isWindows = check(/windows|win32/), isMac = check(/macintosh|mac os x/), isAir = check(/adobeair/), isLinux = check(/linux/), isSecure = /^https/i.test(window.location.protocol); if (isIE6) { try { DOC.execCommand("BackgroundImageCache", false, true);//解决ie6下背景图片不能缓存问题 } catch(e) {} }
读取cookie值
function getCookie(name){ var reg = new RegExp("(^|; )"+name+"=([^;]*)","gi"),r = null; document.cookie.replace(reg,function(a,b,c){r = c}); return r; }
匹配某些内容
var htmlRe=/<html[^>]*>((\n|\r|.)*?)<\/html>/ig, scriptRe=/(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig, styleRe=/(?:<style([^>]*)?>)((\n|\r|.)*?)(?:<\/style>)/ig, hrefRe=/(?: href=[\"']*)([^\"' ]+)(?:[\"' ])/ig, emptyRe=/(?:<--)((\n|\r|.)*?)(?:-->)/ig, ydphpne=/^((13[4-9])|(15[012789])|147|182|187|188)[0-9]{8}$/,//移动手机号码 allphpne=/^((13[0-9])|(15[0-9])|(18[0-9]))[0-9]{8}$/,//所有手机号码 ltphpne=/^((13[0-2])|(15[56])|(186)|(145))[0-9]{8}$/,//联通手机号码 dxphpne=/^((133)|(153)|(180)|(189))[0-9]{8}$/,//电信手机号码
js获取参数值
function getUrlParam(name,url){ url = ("" + url) || location.search; var arr = [],obj = {}; if(name){ name = name.replace(/[\[\]]/g, "\\$&"); url.replace(new RegExp("[&?]"+ name + "(=?([^&#]*)|&|#|$)","ig"), function($,$1,$2) { arr.push(decodeURIComponent($2 == undefined ? "":$2)); }); return arr.length > 1 ? arr : arr.join(""); }else{ url.replace(/[?&]([^&#=]+)/g, function($,$1){ !obj[$1] && (obj[$1] = getUrlParam(url, $1)); }); return obj; } }
时间格式化
function formatDate(t,str){ if(t.toUTCString() == "Invalid Date"){ return "error"; }else{ var obj = { yyyy:t.getFullYear(), yy:(""+ t.getFullYear()).slice(-2), M:t.getMonth()+1, MM:("0"+ (t.getMonth()+1)).slice(-2), d:t.getDate(), dd:("0" + t.getDate()).slice(-2), H:t.getHours(), HH:("0" + t.getHours()).slice(-2), m:t.getMinutes(), mm:("0" + t.getMinutes()).slice(-2), s:t.getSeconds(), ss:("0" + t.getSeconds()).slice(-2), w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()] }; return str.replace(/([a-z]+)/ig,function($1){return obj[$1]}); } }
正侧获取相同class节点
document.getElementsByClassName = function (className, obj) { var o= obj || document; var c = o.getElementsByTagName('*') || document.all; var a = []; var r=new RegExp('\\b'+className+'\\b','i'); for (var i = 0; i < c.length; i++) { if(r.test(c[i].className)) a.push(c[i]); } return a; }
关键词: 正则表达式,javascript 编辑时间: 2017-03-12 21:59:32
0
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 暂无评论
文章图片 article Pictrue
网友评论