标签:
var str = ‘20160925120014‘; var date_str = str.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/g,‘$1-$2-$3 $4:$5:$6‘); console.log(new Date(date_str).getFullYear()+"-"+(new Date(date_str).getUTCMonth()+1)+"-"+new Date(date_str).getDate()+" "+new Date(date_str).getHours()+":"+new Date(date_str).getMinutes()+":"+new Date(date_str).getSeconds());
1.正则表达式
/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/g,‘$1-$2-$3 $4:$5:$6‘
此表达式是意思是第一组匹配连续4个数字,第二组匹配连续两个数字,第三组匹配连续两个数字,第四组匹配连续两个数字,第五组匹配连续两个数字,第六组匹配连续两个数字,然后把匹配到的6组数字分别存到$1,$2,$3,$4,$5,$6中
2.对于正则replace约定了一个特殊标记符$:
下面来几个demo:
"boy & girl".replace(/(\w+)\s*&\s*(\w+)/g,"$2 & $1") //girl & boy
"boy"
.replace(/\w+/g,
"$&-$&"
)
// boy-boy
"javascript"
.replace(/script/,
"$& != $`"
)
//javascript != java
"javascript"
.replace(/java/,
"$&$‘ is "
)
// javascript is script
标签:
原文地址:http://www.cnblogs.com/iagw/p/5909695.html