正则表达式的各种用法
2016-08-30 17:30
141 查看
表单验证只能输入数字和点,否则就输入不进去,用的事件是keyup
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("$#$",".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
obj.value = obj.value.replace(/\d{14,}/g, obj.value.slice(0,14));
给数字加千分符
<script language="JavaScript" type="text/javascript">
function formatNumber(num){
if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){
return num;
}
var a = RegExp.$1,b = RegExp.$2,c = RegExp.$3;
var re = new RegExp("(\\d)(\\d{3})(,|$)");
while(re.test(b)){
b = b.replace(re,"$1,$2$3");
}
return a +""+ b +""+ c;
}
var num=3243245134151343.18787;
console.log(formatNumber(num))
/*alert("num="+num+",四舍五入:"+Math.round(num)+",两位有效数字:"+num.toFixed(2)+",添加千位分隔符:"+formatNumber(num)); */
</script>
获取href里面带的某一个参数
uri =' http://community.antfortune.net/community/comment.htm?commentId=2016090102900100017010&topicId=2088102147230111' var value = uri.match(new RegExp('[\?\&]' + param + '=([^\&]*)(\&?)', 'i')); //param是找的关键字例如commentId
(value里输出来的值如右侧 ["?commentId=2016090102900100017010&", "2016090102900100017010", "&"] )
return value ? decodeURIComponent(value[1]) : value;
匹配图片var regImg=/(<img\b[^>]*src\s*=\s*"[^>"]*\.(?:png|jpg|bmp|gif)"[^>]*>)/;
后端返回的可能是U_1或S_2等等 var reg = /(\{\{([a-z]_[0-9]+)\}\})+/gi; var str1 = $content.replace(reg, function (r, $1, $2) { if (/^U/.test($2)) { var uIndex = $2.substr($2.length - 1); var ATUSRs = (uIndex==2)? $referenceMap.ATUSR[0].atUserId: $referenceMap.ATUSR[uIndex].atUserId; if(ATUSRs && uIndex!=2){ return '<span class="friends" userid='+$referenceMap.ATUSR[uIndex].atUserId+'>'+$referenceMap.ATUSR[uIndex].referString+'</span>'; }else if(uIndex==2 && ATUSRs){ return '<span class="friends" userid='+$referenceMap.ATUSR[0].atUserId+'>'+$referenceMap.ATUSR[0].referString+'</span>'; }else{ return '<span class="friends" userid="">'+$referenceMap.ATUSR[uIndex].referString+'</span>'; } };
if (/^S/.test($2)) { var uIndex = $2.substr($2.length - 1); return '<span class="atFriend">'+$referenceMap.STOCK[uIndex].referString+'</span>'; };
exec方法 (获取浏览器里的cookie里的某一个key及value值) var m = new RegExp('\\b' + key + '\\=([^;]+)').exec(document.cookie);
var str = "session.cookieNameId=ALIPAYJSESSIONID; ctoken=W7QjPHxCLOeEv1E5; ALIPAYJSESSIONID=GZ005V4hQPczTAWj8zqPq7SyST3mLyauthcenterGZ00"; //str里存的是电脑网页里的cookie的值,想获取cookie里的ctoken的值
/\bctoken\=([^;]+)/g.exec(str) 得出结果 ["ctoken=W7QjPHxCLOeEv1E5", "W7QjPHxCLOeEv1E5"]
注:把上面这行正则改成用new regexp的形式去写就是第一行的那种写法,key是变量,这里就是指ctoken, str里就是document.cookie里的值
String.prototype.escapeHTML = function () { return this.replace(/&/g,'&').replace(/>/g,'>').replace(/</g,'<').replace(/"/g,'"'); }; //将后台返回来的标签元素转变成让标签显示在浏览器上
用正则匹配返回日期 new BaseJs().dateFormat(v, "{y}{mm}{d} {h}:{m}")
dateFormat: function (time, tpl) { var zeroFix = function (d) { if (d < 10) return "0" + d; return d; }; var date = new Date(time), year = date.getFullYear(), years = date.getFullYear(), month = zeroFix(date.getMonth() + 1) months = zeroFix(date.getMonth() + 1), day = zeroFix(date.getDate()), days = zeroFix(date.getDate()), hour = zeroFix(date.getHours()), minute = zeroFix(date.getMinutes()), second = zeroFix(date.getSeconds()); var nowDate = new Date(), nowYear = zeroFix(nowDate.getFullYear()), nowMonth = zeroFix(nowDate.getMonth() + 1), nowDay = zeroFix(nowDate.getDate()); return tpl.replace(/\{([^{}]+)\}/g, function (a, b) { switch (b) { case "y": year = (years == nowYear)? "": years+"-"; return year; case "mm": if(years == nowYear && months == nowMonth && days == nowDay){ month = "今天"; }else if (years == nowYear && months == nowMonth && days == nowDay-1){ month = "昨天"; }else{ month = month+"-"; }; return month; case "d": if(years == nowYear && months == nowMonth && days == nowDay || years == nowYear && months == nowMonth && days == nowDay-1){ day = ""; } return day; case "h": return hour; case "m": return minute; case "s": return second; } }); },
相关文章推荐
- 【AS3代码】正则表达式的各种用法和小实例
- 正则表达式的用法
- C#中正则表达式的用法
- c#正则表达式的用法
- 正则表达式教程-各种操作符的运算优先级(3)
- RegExp对象正则表达式功能用法
- 正则表达式的用法
- 转载:Python正则表达式的用法(2. 替换;3.拆分)
- 正则表达式基本用法B
- sscanf 的正则表达式用法
- 正则表达式的用法
- Python正则表达式的用法(2. 替换;3.拆分)
- 正则迷雾,各种工具之正则表达式语法比较
- javascript正则表达式到VBScript正则表达式的转换,兼谈VB里正则表达式的用法
- VBS中的正则表达式的用法
- 正则表达式的用法
- JScript中正则表达式用法详解(附例子:JScript多语言语法加亮引擎)
- 正则表达式的一些用法
- 正则表达式用法
- 转载:Python正则表达式的用法(1. 匹配)