`

JS正则格式化日期,在字符串中插入内容

    博客分类:
  • js
阅读更多
var getBirthdayFromIdCard = function(idCard){
	var birthday = "";
	if(idCard != null && idCard != ""){
		if(idCard.length == 15){
			birthday = "19"+idCard.substr(6,6);
		} else if(idCard.length == 18){
			birthday = idCard.substr(6,8);
		}
		
		birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
	}
	return birthday;
}

alert(getBirthdayFromIdCard("130503670401001"));
// 1967-04-01

alert(getBirthdayFromIdCard("420621188905021111"));
// 1989-05-02

 

 

var str = 'abcdefg';
str = str.replace(/(.{3})/,"$1BBB");
alert(str);//abcBBBdefg

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics