早教吧 育儿知识 作业答案 考试题库 百科 知识分享

正则表达式y+Date.prototype.Format=function(fmt){//author:meizzvaro={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"h+":this.getHours(),//小时"m+":this.getMinutes(),//分"s+":this.getSeconds(),//秒"q+":Math.floor(

题目详情
正则表达式y+
Date.prototype.Format = function(fmt)
{ //author:meizz
var o = {
"M+" :this.getMonth()+1,//月份
"d+" :this.getDate(),//日
"h+" :this.getHours(),//小时
"m+" :this.getMinutes(),//分
"s+" :this.getSeconds(),//秒
"q+" :Math.floor((this.getMonth()+3)/3),//季度
"S" :this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1,(RegExp.$1.length==1) (o[k]) :(("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
在网上看到的这段JS代码.想问下大家这句 if(/(y+)/.test(fmt))
/(y+)/是什么意思呢 ,
▼优质解答
答案和解析
正则表达式中y只是一个普通文本,用于匹配字母y
y+的意思是:匹配1个到多个y
(y+)的意思是:y+匹配到的内容可能通过分组来取到,这里是通过第一个分组取到.从后面的代码中可以看出,RegExp.$1就是取到的y+匹配到的内容
看了 正则表达式y+Date.pr...的网友还看了以下: