<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css">
fieldset {
padding:10px;
}
.button{
width:100px;
}
#runTimes{
color:red;
font-size:12px;
}
</style>
<script type="text/javascript">
/*
*/
var src //测试字符串
var re //正则表达式
var execIndex = -1;
var arr;
var rng;
function getPattern(){
var o = $('Pattern')
o.style.color='blue';
o.style.fontWeight='';
o.style.fontStyle ='';
try{
re = new RegExp(o.value.replace(/\r\n/img,'\r'),getflags())
resetSearch()
}catch(e){
o.style.color="red";
//o.style.fontWeight="bold";
//o.style.fontStyle = "italic";
}
}
function getString(o){
src = o.value.replace(/\r\n/img,'\r');
}
function resetSearch(){
arr = undefined;
execIndex = -1;
re = new RegExp(re);
$("runTimes").innerText = '';
$("result").options.length = 0;
}
function getflags(){
var f = '';
if ($("IgnoreCase").checked)
f +='i';
if ($("global").checked)
f +='g';
if ($("MultiLine").checked)
f +='m';
return f;
}
function getResult(value){
var objResult = $("result");
objResult.options.length = 0;
objResult.options.add(new Option('$0='+value[0],0))
for (var i=1;i<value.length;i++){
objResult.options.add(new Option('$'+i+'='+value[i],i))
}
}
function gofind(){
if ($("Pattern").style.color=='red') { alert('错误的正则表达式!');return}
if (!re) {return}
var timebgein = (new Date()).getTime();
//alert(src+','+arr+','+re);
var matchCount = src.match(new RegExp(re))
if ((arr = re.exec(src)) != null){
getResult(arr);
var timeend = (new Date()).getTime();
execIndex ++;
selectText(arr.index,arr.lastIndex);
$("runTimes").innerText = "第"+(execIndex+1)+'/'+matchCount.length+"个匹配(" + eval(timeend - timebgein) + "ms)";
}else{
resetSearch();
if (matchCount){
$("runTimes").innerText = "匹配完成~!"
}else{
$("runTimes").innerText = "没有找到匹配~!"
}
selectText(src.length,src.length)
}
}
function selectText(sp,ep,fText)
{
sp = parseInt(sp)
ep = parseInt(ep)
if(isNaN(sp)||isNaN(ep))
return;
//alert(sp+','+ep);
rng.moveEnd("character",-src.length)
rng.moveStart("character",-src.length)
rng.collapse(true);
rng.moveEnd("character",ep)
rng.moveStart("character",sp)
if (fText) rng.findText(fText)
rng.select();
}
function selectResult($index){
try{
var i=0;
var o;
var $re = new RegExp(re);
for (var i=0;i<=execIndex;i++){
o = $re.exec(src)
}
selectText(o.index,o.lastIndex,o[$index]);
}catch(e){}
}
function $(o){
return document.getElementById(o);
}
function bodyload(){
rng = $("String").createTextRange()
re = new RegExp('','img')
src='';
arr = null;
}
</script>
</head>
<body οnlοad="bodyload()">
<div class="content">
<div class="toolcontent">
<label for="IgnoreCase"><strong>正则表达式测试工具</strong>
<fieldset><legend></legend>
<table width="102%" height="800" border="0" cellpadding="00" cellspacing="0">
<tr>
<td height="829"><p align="left"><strong>正测表达式</strong>
<textarea name="textarea2" id="Pattern" style="width:98%;height:200px;color:blue" οnkeyup="getPattern()"></textarea>
</p>
<div align="left">
<input name="button" type="button" class="button" οnclick="gofind()" value="匹配" />
<input name="button2" type="button" class="button" οnclick="resetSearch();selectText(0,0);" value="重置" />
<input name="checkbox" type="checkbox" id="IgnoreCase" οnclick="getPattern()" checked="checked" />
忽略大小写
<input name="checkbox" type="checkbox" id="MultiLine" οnclick="getPattern()" checked="checked" />
多行模式
<input name="checkbox" type="checkbox" id="global" οnclick="getPattern()" checked="checked" />
全局模式
<label for="label"></label>
<label for="label2"></label>
<span id="runTimes"></span> </div>
<p><strong>测试字符串</strong>
<textarea name="textarea" id="String" style="width:98%;height:200px" οnchange="getString(this)"></textarea>
</p>
<p><strong>结果 </strong>
<select name="select" size="20" id="result" style="width:100%" οnchange="selectResult(this.value)">
</select>
</p>
</td>
</tr>
</table>
</fieldset>
</div>
</div>
<!--工具列表开始-->
<!--工具列表结束-->
</body>
</html>