﻿ldh={
	ex:function(main,ex){for(var k in (ex||{}))main[k]=ex[k];return main}
	,each:function(list,fn){for(var i=0;i<list.length;i++)fn.call(list[i],i,list)}
	,chk:function $chk(obj){return !!(obj || obj === 0)}
	,find:function(selector,box){
		if(typeof(selector)!='string')return selector;
		if(selector.charAt(0)=='.')return (function(className,box){
			var list=[];
			ldh.each(ldh.find('*',box),function (i){
				if(this.className.indexOf(className)!=-1)list.push(this)
			})
			return list;
		})(selector.replace('.',''),ldh.find(box));
		if(selector.charAt(0)=='#')return document.getElementById(selector.replace('#',''));
		return (ldh.find(box)||document).getElementsByTagName(selector);
	}
};
$=ldh.find;
var Attr=function (key,value){return value?(this.setAttribute(key,value),this):this.getAttribute(key)};
var commad=function(){},prevPos=0,resultList=[];
ldh.each(ldh.find('INPUT'),function (){this.onclick=applySearch})
function applySearch(){
	var mode='',re,beginTime=new Date;
	ldh.each(ldh.find('INPUT'),function (){if(this.checked)mode+=Attr.call(this,'m')});
	if($('#regexpText').value==''){re=null}
	else{try{re=new RegExp($('#regexpText').value,mode)
		}catch(e){re=null}	
	}
	var mArr=String($('#targetText').value).match(re);
	$('#resultText').value='';
	if(!(mArr instanceof Array))mArr=[];
	resultList=mArr;
	var time='共耗时 <strong style="color:#009900">'+(new Date-beginTime)+"</strong> 毫秒"
	command(mArr,re,mode,time);
}
if(document.all){
	var rngTarget=$('#targetText').createTextRange(),firstRng=rngTarget.getBookmark();
}
function findText(text){ 
	if(!text)return;
	rngTarget.collapse(true); 
	if(rngTarget.findText(text)){ 
		rngTarget.select(); 
		rngTarget.collapse(false); 
	}else{
		rngTarget.moveStart('character',-$('#targetText').value.length);
	} 
} ;
(function (){
	$('#regexpText').onchange=function(){resultList=[];}
	$('#exps1').onchange=
	$('#exps2').onchange=
	$('#exps3').onchange=function (){$('#regexpText').value=this.value;resultList=[]};
	($('#doFind').onclick=function (){
			command=function(mArr,a,b,c){
				$('#reInfo').innerHTML='查找结果(共找到 <strong style="color:red">'+mArr.length+'</strong> 个匹配,'+c+')';
				if(mArr.length==0)return $('#resultText').value='没有找到匹配项';
				var txt=[];
				ldh.each(mArr,function (i){txt.push((i+1)+'. '+this+'\n')});
				$('#resultText').value=txt.join('');
			};
			applySearch();
	})();
	$('#doReplace').onclick=function (){
			command=function (mArr,re,b,c){
				$('#resultText').value=$('#targetText').value.replace(re,$('#replaceText').value);
				$('#reInfo').innerHTML='已替换 <strong style="color:red">'+mArr.length+'</strong> 个匹配项 ('+c+')'
			}
			applySearch();
	};
	$('#doTrim').onclick=function (){
			command=function (mArr,re,b,c){
				$('#resultText').value=$('#targetText').value.replace(re,'');
				$('#reInfo').innerHTML='已剔除 <strong style="color:red">'+mArr.length+'</strong> 个匹配项 ('+c+')'
			}
			applySearch();
	};
	$('#doOut').onclick=function (){
			command=function (mArr,re,mode,c){
				$('#resultText').value=$('#targetText').value.replace(re,'');
				$('#reInfo').innerHTML='JavaScript代码 ('+c+')';
				if(mArr.length==0)return $('#resultText').value='由于没有任何匹配项，所以未能输出正则表达式!';
				mode=mode?',"'+mode+'"':'';
				var txt=$('#regexpText').value.replace(/\\/g,'\\\\')
				$('#resultText').value='myRegExp=new RegExp("'+txt+'"'+mode+');';
			}
			applySearch();
	};
	$('#doNext').onclick=function (){
			if(resultList.length==0){applySearch()}
			var m=resultList.shift()
			findText(m);
			$('#resultText').value=m?m:'没有找到匹配项';
	}
	ldh.each($('A','#tabBtns'),function (i){
		this.index=i;
		this.onkeydown=blur;
		this.onclick=function (){
			var List=$('DIV','#all_list');
			var Btns=$('A','#tabBtns');
			ldh.each(Btns,function(){this.className=''});
			this.className='hot';
			ldh.each(List,function(){this.style.display='none'});
			List[this.index].style.display='block';
		}
	});
	ldh.each([
		{text:'合法帐号',value:'^[a-zA-Z][a-zA-Z0-9_]{4,15}$'	}
		,{text:'图片文件',value:'.*(.jpg|.jpeg|.gif|.png)$'}
		,{text:'E-mail',value:'^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$'}
		,{text:'身份证',value:'^\\d{15}|\\d{18}$'}
		,{text:'QQ号',value:'^[1-9]*[1-9][0-9]*$'}
		,{text:'全中文',value:'^[\\u4e00-\\u9fa5]+$'}
		,{text:'手机号码',value:'^13\\d{9}|15[89]\\d{8}|189\\d{8}$'}
		,{text:'固定电话',value:'^((\\d{3}|\\d{4})-?)?(\\d{8}|\\d{7})$'}
		,{text:'邮政编码',value:'^[1-9]{1}(\\d+){5}$'}
		,{text:'URL',value:'http:\/\/([\\w-]+\\.)+[\\w-]+(\/\\[\\w- .\/?%&=\\]*)?'}
		,{text:'IP地址',value:'^(\\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])\\.(\\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])$'}
		,{text:'数字',value:'^[+|-]?\\d*\\.?\\d*$'}
		,{text:'正整数',value:'^[1-9]\\d*$'}
		,{text:'负整数',value:'^-[1-9]\\d*$'}
		,{text:'任意整数',value:'^-?[1-9]\\d*$'}
		,{text:'正浮点数',value:'^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$'}
		,{text:'负浮点数',value:'^-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*)$'}
		,{text:'任意浮点数',value:'^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$'}
		,{text:'英文字母',value:'^[A-Za-z]+$'}
		,{text:'小写英文字母',value:'^[a-z]+$'}
		,{text:'大写英文字母',value:'^[A-Z]+$'}
	],function (){var O=$('#exps1').options;O[O.length]=new Option(this.text,this.value)});
	ldh.each([
		{text:'img标签',value:'<img[^>]+>'	}
		,{text:'链接',value:'<a[^>]+>(.*?)<\\/a>'}
		,{text:'图片地址',value:'(s|S)(r|R)(c|C) *= *(\\\'|\\\")?(\\w|\\\\|\\/|\\.|:)+(\\4)'}
		,{text:'链接地址',value:'href=(\\"|\\\'|)([^\\"\\\'<>]+)(\\1)'}
		,{text:'中文字符',value:'[\\u4e00-\\u9fa5]'}
		,{text:'双字节字符',value:'[^\\x00-\\xff]'}
		,{text:'文件名',value:'([^\\\/]*?)(?:\\.\\w+)$'}
	],function (){var O=$('#exps2').options;O[O.length]=new Option(this.text,this.value)});
	ldh.each([
		{text:'空行',value:'\\n[\\s| ]*\\r'}
		,{text:'HTML标签',value:'<[^>]+>'}
		,{text:'首尾空格',value:'(^\\s*)|(\\s*$)'}
	],function (){var O=$('#exps3').options;O[O.length]=new Option(this.text,this.value)});
	if(!document.all)$('#doNext').disabled=true;
})()
