一个测试正则表达式的脚本
一个周末,在家里看正则表达式的资料,后来突发其想,做了这样的一个脚本页面,用来测试正则表达式。
<html>
<head><title>正则表达式测试(V0.1)</title>
<style>
body, td {font-size: 12}
.big {font-size: 14}
.regexp {font-size: 14; color: #ff0000}
.input {font-size: 14; color: #808080}
.table {border-left: 1px solid #808080; border-top: 1px solid #808080; height: 40}
.td {border-top: 1px solid #808080}
.column {border-top: 1px solid #808080; border-right: 1px solid #808080}
.line {border: 1px solid #808080; height: 40}
</style>
<script type="text/javascript">
function testRegexp() {
var d, f, i, m, o, r, v;
with (document.all) {
d = displace.value;
m = method.value;
r = regexp.value;
v = value.value;
f = flag;
i = input;
}
if (r != "") {
var p = "";
if (f != undefined) {
for (var j=0; j<f.length; j++)
p += (f[j].checked)?f[j].value:"";
try {
o = (p == "")?new RegExp(r):new RegExp(r, p);
} catch (err) {
alert("Error:" + err.number + ":" + err.description);
}
switch (m) {
case "exec":
i.value = o.exec(v);
break;
case "test":
i.value = o.test(v);
break;
case "match":
i.value = v.match(o);
break;
case "replace":
i.value = v.replace(o, d);
break;
case "search":
i.value = v.search(o);
break;
default:
i.value = "";
}
}
}
}
function clearRegexp() {
if (confirm("您确定要清空当前所有表单中的内容?")) {
var f;
with (document.all) {
regexp.value = "";
value.value = "";
input.value = "";
displace.value = "";
f = flag;
}
if (f != undefined) {
for (var i=0; i<f.length; i++)
f[i].checked = false;
}
}
}
function testMethod() {
var m, r, e;
with (document.all) {
e = empty;
m = method.value;
r = swap;
}
if (e!= undefined && r != undefined) {
if (m == "replace") {
e.style.width = "7%";
r.style.display = "";
} else {
e.style.width = "28%";
r.style.display = "none";
}
}
}
</script>
</head>
<body onload="testMethod()">
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><br/><br/><font class="big">正则表达式测试(V0.1)</font><br/><br/></td>
</tr>
<tr>
<td align="center">
<table border="0" width="600" cellpadding="0" cellspacing="0">
<tr>
<td class="table" width="12%" align="center">表达式:</td>
<td class="column" width="88%" colspan="4"><input type="text" name="regexp" size="71" class="regexp"></td>
</tr>
<tr>
<td class="table" align="center">测试值:</td>
<td class="column" colspan="4"><textarea name="value" cols="70" rows="5"></textarea></td>
</tr>
<tr>
<td class="table" align="center">输出值:</td>
<td class="column" colspan="4"><textarea name="input" cols="70" rows="2" class="input" readonly></textarea></td>
</tr>
<tr>
<td class="table" align="center">标 识:</td>
<td class="td" width="28%"><input type="checkbox" name="flag" value="g" onfocus="this.blur()">全局 <input
type="checkbox" name="flag" value="i" onfocus="this.blur()">忽略 <input type="checkbox" name="flag" value="m"
onfocus="this.blur()">多行</td>
<td class="td" id="empty" width="28%"> </td>
<td class="td" width="10%" align="center">方 法:</td>
<td class="column"><select name="method" onchange="testMethod()">
<option value="exec">exec()方法</option>
<option value="test">test()方法</option>
<option value="match">match()方法</option>
<option value="replace">replace()方法</option>
<option value="search">search()方法</option></select><span id="swap" style="display: none"> <input
type="text" name="displace" size="14"></span>
</td>
</tr>
<tr>
<td class="line" align="center" colspan="5"><input type="button" value="测试开始" onclick="testRegexp()"
onfocus="this.blur()"> <input type="button" value="清空内容"
onclick="clearRegexp()" onfocus="this.blur()"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>