正則練習(xí)兼文本處理器 2.0


<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">
<title>梅雪香?--?正則練習(xí)兼文本處理器?2.0</title>
<style?type="text/css">


#divMain
{width:100%;?text-align:center;}

#tbVector
{width:800px;?height:500px;?font-size:12px;?border:1px?solid?#CC9900;?margin-bottom:4px;}

#tbVector?td
{border:1px?solid?#CC9900;?width:50%;?}

textarea
{
????width:100%;?
????height:100%;?
????background-color:#CCFFCC?;
????scrollbar-highlight-color:?#CCCC99;
????scrollbar-shadow-color:?#8C9EB7;
????scrollbar-3dlight-color:?#B0C4D8;
????scrollbar-track-color:?#CCCC88;
????scrollbar-Base-Color:?#CCCC99;
}


#tr1,#tr3,#tr5
{height:25px;?color:#FF0066;?font-size:13px;?font-weight:?bold;?cursor:default;?}

#tr2
{height:40px;}

#tr1?td,#tr3?td
{?background-color:#FFCCFF?}


#spRegTitle
{width:190px;}

#spOption
{width:100px;}

#spHisRecord
{width:60px;?font-weight:?600;?cursor:hand;?color:blue;?text-align:right;}


#tr5
{background-color:FFFFEF}

#tr5?td
{text-align:center;}

#tr5?#tr5td2?input
{?width:60px;?border:1px?solid?#66CCFF;?background-color:#FF99CC}

#spSou,#spOut
{?width:280px;?}

#spReadFromFile,#spWrToFile
{?color:blue;?cursor:hand;?}


#tbDollar
{width:800px;?font-size:12px;?border:1px?solid?#CC9900;?cursor:default;?margin-bottom:4px;}


#tdR1,#tdR2,#tdR3,#tdR4,#tdR5,#tdR6,#tdR7,#tdR8,#tdR9
{width:60px;?text-align:center;?vertical-align:middle;??}

#tdD1,#tdD2,#tdD3,#tdD4,#tdD5,#tdD6,#tdD7,#tdD8,#tdD9
{width:207px;?word-break:break-all;?}


#tdR1,#tdR2,#tdR3,#tdD4,#tdD5,#tdD6,#tdR7,#tdR8,#tdR9
{?background-color:#BFD1D9;?}

#tdD1,#tdD2,#tdD3,#tdR4,#tdR5,#tdR6,#tdD7,#tdD8,#tdD9
{?background-color:#D8C0D8;?}


#divCopyRight
{?width:800px;?height:30px;?text-align:right;?color:blue;font-size:12px;?}


#divHisRecd
{?border:1px?solid?#CC9900;?position:absolute;?background-color:#D8C0D8;display:none;?cursor:default;??}

#divHisRecd?div
{font-size:12px;??margin:2px;?}

.divList0
{background-color:#D8F7F8;?}

.divList1
{background-color:#FEFEE7;?}

.divListOver
{background-color:#FF9933;?}
</style>
<script?language="JavaScript">
<!--

/**//*程序:正則練習(xí)兼文本處理器
?*作者:梅雪香(meixx)
?*日期:
?*版本:2.0
*/

var?$?=?document.getElementById;
var?gFso?=?null;?//全局FileSystemObject對象
var?arHisRecd?=?[];?//?記錄使用過的正則歷史
var?lastStyle?=?"";

//獲得對象的位置信息

function?getPosition(obj)?
{
????var?top=0;
????var?left=0;
????var?width=obj.offsetWidth;
????var?height=obj.offsetHeight;

????while?(obj.offsetParent)?
{
????????????top?+=?obj.offsetTop;
????????????left?+=?obj.offsetLeft;
????????????obj?=?obj.offsetParent;
????????}

????return?
{"top":top,"left":left,"width":width,"height":height};
}

//全局初始化

function?init()
{
????var?divHisRecd?=?$("divHisRecd");
????var?o?=?getPosition($("spHisRecord"));

????with(divHisRecd.style)
{
????????top?=?o.top?+?o.height;
????????left?=?o.left;
????}

????with(divHisRecd)
{

????????attachEvent("onselectstart",function()
{event.returnValue?=?false;});

????????attachEvent("oncontextmenu",function()
{event.returnValue?=?false;});
????}


????$("spWrToFile").attachEvent("onclick",function()
{
????????fso?=?new?ActiveXObject("Scripting.FileSystemObject");
????????var?val?=?$("filText").value;
????????if(val?==?"")?return;
????????var?ts?=?fso.OpenTextFile(val,?2);
????????ts.write(?$("taOut").value);
????????ts.close();
????????alert("文件\n"?+?val?+?"\n替換并且保存成功!");
????});


????$("filText").attachEvent("onchange",function()
{
????????gFso?=?new?ActiveXObject("Scripting.FileSystemObject");
????????var?val?=?$("filText").value;
????????var?file?=?gFso.GetFile(val);
????????var?ts?=?gFso.OpenTextFile(val,?1);
????????$("taSou").value=?(file.Size>0?ts.ReadAll():"");
????????ts.close();
????});


????document.onclick?=?function()
{
????????

????????switch(event.srcElement.id)
{
????????????case?"spHisRecord":?

????????????????(function()
{
????????????????????if(arHisRecd.length)
????????????????????????divHisRecd.style.display?=?(divHisRecd.style.display?==?"block"?)?"none":"block";?
????????????????})();
????????????????break;
????????????case?"btnTest":
????????????case?"btnMatch":
????????????case?"btnReplace":

????????????????(function()
{
????????????????????var?strReg?=?$("taReg").value;
????????????????????for(var?i=0;i<arHisRecd.length;i++)
????????????????????????if(strReg?==?arHisRecd[i])
????????????????????????????return;
????????????????????arHisRecd.push(strReg);
????????????????????var?oDiv?=?document.createElement("div");
????????????????????oDiv.innerText?=?strReg?;
????????????????????oDiv.className?=?"divList"?+?arHisRecd.length?%?2;

????????????????????oDiv.attachEvent("onmouseover",function()
{
????????????????????????????????????????????????????????lastStyle?=oDiv.className;?
????????????????????????????????????????????????????????oDiv.className?=?"divListOver";?});

????????????????????oDiv.attachEvent("onmouseout",function()
{oDiv.className?=?lastStyle;?});

????????????????????oDiv.attachEvent("onclick",function()
{?
????????????????????????????????????????????????????$("taReg").value?=?oDiv.innerText;?
????????????????????????????????????????????????????divHisRecd.style.display?=?"none";?});
????????????????????divHisRecd.appendChild(oDiv);
????????????????})();
????????????????break;
????????????case?"spReadFromFile":?$("filText").click();?break;
????????????default:??break;
????????}
????}
}

//根據(jù)taReg文本域中的內(nèi)容生成正則表達式

function?mkReg()
{
????return?new?RegExp($("taReg").value,($("chkGlobal").checked?"g":"")?+?($("chkIgnoreCase").checked?"i":""));
}

//重置文本域和輸入表格

function?resetAll()
{
????//清空文本域
????var?tas?=?["taReg","taRep","taOut","taSou"];
????for(var?i=0,j=tas.length;i<j;i++)
????????$(tas[i]).value?=?"";
????//清空$1-$9
????for(i=1;i<10;i++)
????????$("tdD"+i).innerText?=?"";
}


function?output$1_$9()
{
????for(var?i=1;i<10;i++)
????????$("tdD"?+?i).innerText?=?eval("RegExp.$"?+?i);
}

//產(chǎn)生0-seed之間的隨機數(shù)

function?rand(seed)
{
????return?Math.floor(Math.random()*seed);
}

//產(chǎn)生隨機color

function?mkRandClr()
{
????var?arr?=?[];
????for(var?i=0;i<6;i++)
????????arr.push("0123456789ABCDDD".charAt(rand(16)));
????return?"#"?+?arr.join("");
}

//Test按鈕點擊處理函數(shù),用正則表達式匹配文本

function?regTest()
{
????var?oA?=?document.createElement("a");
????oA.innerHTML?=?"<font?color='"?+?mkRandClr()?+?"'>"?+?mkReg().test($("taSou").value)?+?"</font>";

????with($("taOut"))
{
????????value?=?"";
????????appendChild(oA);
????}
????output$1_$9();
????if($("chkAutoCopy").checked)?$("btnCpyResult").click();
}

//Match按鈕點擊處理函數(shù),用正則表達式匹配文本

function?regMatch()
{
????$("btnTest").click();
????var?ar?=?$("taSou").value.match(mkReg());

????if(ar)
{
????????$("taOut").value?=?ar.join("\r\n");
????????if($("chkAutoCopy").checked)?$("btnCpyResult").click();
????}else
????????$("taOut").value?=?"沒有匹配到的結(jié)果!";
}

//Replace按鈕點擊處理函數(shù),用正則表達式匹配文本

function?regReplace()
{
????$("btnTest").click();
????$("taOut").value?=?$("taSou").value.replace(mkReg(),$("taRep").value);
????if($("chkAutoCopy").checked)?$("btnCpyResult").click();
}

//復(fù)制替換結(jié)果

function?copyResult()
{
????window.clipboardData.setData("text",$("taOut").value);
}
//-->
</script>
</head>
<body?onLoad="init()">
<div?id="divHisRecd"></div>
<div?id="divMain">
????<table?id="tbVector">
????????<tr?id="tr1">
????????????<td>
????????????????<span?id="spRegTitle">正則字符串</span>
????????????????<span?id="spOption">
????????????????????<input?id="chkGlobal"?type="checkbox"?id="chkGlobal"?checked="true"?title="選中全局匹配">g
????????????????????<input?id="chkIgnorecase"?type="checkbox"?id="chkIgnoreCase"?title="選中不區(qū)分大小寫">i
????????????????</span>
????????????????<span?id="spHisRecord"?title="打開正則歷史記錄">正則列表</span>
????????????</td>
????????????<td>替換字符串</td>
????????</tr>
????????<tr?id="tr2">
????????????<td><textarea?id="taReg"?rows="2"?cols="100%">(^|;|\s)name\s*:\s*([^;]*?)(\s|;|$)</textarea></td>
????????????<td><textarea?id="taRep"?rows="2"?cols="100%"></textarea></td>
????????</tr>
????????<tr?id="tr3">
????????????<td>
????????????????<span?id="spSou">待處理字符串</span>
????????????????<span?id="spReadFromFile"?title="從文件讀取字符串">
????????????????<input?type="file"?id="filText"?style="display:none"?/>從文件讀入</span>
????????????</td>
????????????<td>
????????????????<span?id="spOut">處理結(jié)果輸出</span>
????????????????<span?id="spWrToFile"?title="把輸出文本域中的內(nèi)容寫回文本">向文件寫入</span>
????????????</td>
????????</tr>
????????<tr?id="tr4">
????????????<td><textarea?id="taSou">id:25;?name:?meixx;?sex:?male</textarea></td>
????????????<td><textarea?id="taOut"></textarea></td>
????????</tr>
????????<tr?id="tr5">
????????????<td?id="tr5td1">
????????????????<input?type="checkbox"?id="chkAutoCopy"?title="選中后自動復(fù)制處理結(jié)果"?checked="true">自動復(fù)制
????????????</td>
????????????<td?id="tr5td2">
????????????????<input?type="button"?id="btnClear"?onClick="resetAll()"?value="Clear"?title="清空所有文本域">
????????????????<input?type="button"?id="btnTest"?onClick="regTest()"?value="Test"?title="用正則測試源字符串">
????????????????<input?type="button"?id="btnMatch"?onClick="regMatch()"?value="Match"?title="用正則匹配源字符串">
????????????????<input?type="button"?id="btnReplace"?onClick="regReplace()"?value="Replace"?title="把正則匹配結(jié)果替換為待替換字符串">
????????????????<input?type="button"?id="btnCpyResult"?onClick="copyResult()"?value="Copy"?title="復(fù)制結(jié)果">
????????????</td>
????????</tr>
????</table>
????<table?id="tbDollar">
????????<tr>
????????????<td?id="tdR1">RegExp.$1</td>
????????????<td?id="tdD1"></td>
????????????<td?id="tdR2">RegExp.$2</td>
????????????<td?id="tdD2"></td>
????????????<td?id="tdR3">RegExp.$3</td>
????????????<td?id="tdD3"></td>
????????</tr>
????????<tr>
????????????<td?id="tdR4">RegExp.$4</td>
????????????<td?id="tdD4"></td>
????????????<td?id="tdR5">RegExp.$5</td>
????????????<td?id="tdD5"></td>
????????????<td?id="tdR6">RegExp.$6</td>
????????????<td?id="tdD6"></td>
????????</tr>
????????<tr>
????????????<td?id="tdR7">RegExp.$7</td>
????????????<td?id="tdD7"></td>
????????????<td?id="tdR8">RegExp.$8</td>
????????????<td?id="tdD8"></td>
????????????<td?id="tdR9">RegExp.$9</td>
????????????<td?id="tdD9"></td>
????????</tr>
????</table>
????<div?id="divCopyRight">Made?By?梅雪香(Meixx)?<a?href="mailto:wy_hd@163.com"?title="報告BUG">BUG</a></div>
</div>
</body>
</html>

posted on 2006-09-28 01:12
梅雪香 閱讀(616)
評論(2) 編輯 收藏