正则示例1

Posted qiqi105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则示例1相关的知识,希望对你有一定的参考价值。

var str="2015-9-22 13:12:0";
str+=‘ ‘;
var ary=["年","月","日","时","分","秒"];
var reg=/(\d+)(-)(\d+)(-)(\d+)(\s)(\d+)(:)(\d+)(:)(\d+)(\s)/g;
var arr=[];
var res=str.replace(reg,function(){
var i=1;
var j=1;
var n=0;
while(i<=12){
arr.push(arguments[i]);
i++;
}

while(n<10){
n=j*2;
j++;
arr[n]=ToDouble(arr[n]);
}

for (var i = 0; i < ary.length; i++) {
var num=i*2+1;
arr[num]=ary[i];
}

function ToDouble(num){
return num>10?num:"0"+num;
}

var res=arr.join(‘‘);

return res;
});
console.log(res);


var str="zhu feng pei xun";//首字母大写
var reg=/\s([a-z])/g;
var res=str.replace(reg,function(){
return arguments[1].toUpperCase();
});
var reg=/[a-z]/;
var res=res.replace(reg,function(){
return arguments[0].toUpperCase();
});
console.log(res);

console.log(‘-----‘);
var str="2015-09-22 13:12:00",reg=/^(\d{4})[-](\d{1,2})[-](\d{1,2})[\s](\d{1,2})[:](\d{1,2})[:](\d{1,2})$/g,ary=[];
str.replace(reg,function(){
ary=[].slice.call(arguments).splice(1,6);
});
//var ary=["年","月","日","时","分","秒"];
var ResStr="{0}年{1}月{2}日 {3}时{4}分{5}秒";
//var ary=["2015","9","22","13","12","0"];
var reg=/{(\d)}/g;
var res=ResStr.replace(reg,function(){
//console.log(arguments);
var num=arguments[1],val=ary[num];
val.length===1?val="0"+val:void 0;
return val;
});
console.log(res);

以上是关于正则示例1的主要内容,如果未能解决你的问题,请参考以下文章

正则表达式的20个示例

正则示例1

(转)Java正则表达式的语法与示例

常用正则表达式示例

Shell编程之正则表达式

python正则匹配示例