哪些自己曾经害怕的正则表达式以及一个万能的校验

前端之家收集整理的这篇文章主要介绍了哪些自己曾经害怕的正则表达式以及一个万能的校验前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var strSrc = "xxa1b01c001yya2b02c002zz";
var re = /a(\d)b(\d{2})c(\d{3})/gi;

//获得匹配的字符串数组
function getMatchString(strSrc,re) {
var array;

array = strSrc.match(re);

for ( var int = 0,j = array.length; int < j; int++) {
var array_element = array[int];

if (int < j - 1)
document.write("[\"" + array[int] + "\",");
else
document.write("\"" + array[int] + "\"]");

}
}




var str ="我.. ..我 ...要... 要学编程";
//去除啰嗦
function removeToomuchWord(str)
{
var str2= str.replace(/\./g,"").replace(/\s/g,"").replace(/(.)\1/g,"$1");
document.writeln(str2);
}



var str = "win a window";
//替换 window win a book 替换win 为 lost 结果为 window lost a book
function replace(str)
{
document.write(str.replace(/\bwin\b/g,"lose"));
}



var str = "http://www.baidu.com:8080/0912.html";
//获得url的各部分信息
function getUrlPartInfo(str)
{

var reg =/^(\w+):\/\/([^:/]*)(?::(\d*))?([^#]*)$/;
var arrays = str.match(reg);
var strInfo ="" ;
for(var index in arrays)
strInfo += arrays[index]+",";
alert(strInfo);
}



//万能的校验器

//以后只需要添加相应的正则表达式就行了,代码不用修改


var patterns = new Object();

patterns.ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.\d{1,2}|1\d\d|2[0-4]\d|25[0-5]){3}$/;
patterns.email = /^(\w+)@(\w+)(\.\w+)+$/;
patterns.date = /^(\d{4})-(0?[0-9]|1[0-2])-(0?[0-9]|[0-2]\d|3[0-1])$/;
// 23:23:59
patterns.time = /^([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)$/;

function verify(str,pat)

{

var pattern = patterns[pat];

if (str.match(pattern))
return true;
else
return false;

}

getMatchString(strSrc,re);

removeToomuchWord(str);

replace(str);

getUrlPartInfo(str)

alert(verify("26.1.123.2","ip") + "," + verify("hilishuai.com","email") + "," + verify("2003-13-31","date") + "," + verify("23:60:22","time"));

猜你在找的正则表达式相关文章