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);
}
//万能的校验器
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")); 原文链接:https://www.f2er.com/regex/362563.html