正则练习兼文本处理器 2.0
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>梅雪香 -- 正则练习兼文本处理器 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">
<!--
/**//*程序:正则练习兼文本处理器
*作者:梅雪香(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;
}
}
}
//根据taReg文本域中的内容生成正则表达式
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);
}
//产生0-seed之间的随机数
function rand(seed){
return Math.floor(Math.random()*seed);
}
//产生随机color
function mkRandClr(){
var arr = [];
for(var i=0;i<6;i++)
arr.push("0123456789ABCDDD".charAt(rand(16)));
return "#" + arr.join("");
}
//Test按钮点击处理函数,用正则表达式匹配文本
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按钮点击处理函数,用正则表达式匹配文本
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 = "没有匹配到的结果!";
}
//Replace按钮点击处理函数,用正则表达式匹配文本
function regReplace(){
$("btnTest").click();
$("taOut").value = $("taSou").value.replace(mkReg(),$("taRep").value);
if($("chkAutoCopy").checked) $("btnCpyResult").click();
}
//复制替换结果
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="选中不区分大小写">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">处理结果输出</span>
<span id="spWrToFile" title="把输出文本域中的内容写回文本">向文件写入</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="选中后自动复制处理结果" checked="true">自动复制
</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="把正则匹配结果替换为待替换字符串">
<input type="button" id="btnCpyResult" onClick="copyResult()" value="Copy" title="复制结果">
</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
梅雪香 阅读(612)
评论(2) 编辑 收藏