-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
207 lines (197 loc) · 5.32 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> 正则表达式测试工具 </title>
<meta name="author" content="白一梓([email protected])">
<meta name="keywords" content="正则表达式测试工具">
<meta name="description" content="简易的正则表达式测试工具,白一梓制作">
<style>
body {
font-size:13px;
color:#3366FF;
}
.right {
color:#005500;
}
.wrong {
color:#CC0033;
}
#container {
margin:auto;
}
#container table {
margin:auto;
width:550px;
border:1px solid;
}
#container h2 {
text-align:center;
}
#container fieldset {
width:530px;
margin:auto;
padding:10;
border:1px solid;
}
#container div {
margin:auto;
text-align:center;
}
.margin {
height:20px;
}
#footer {
margin:auto;text-align:center;
}
</style>
<script language="javascript">
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}
function setCookie(name,value,expires,path,domain,secure)
{
var expDays = expires*24*60*60*1000;
var expDate = new Date();
expDate.setTime(expDate.getTime()+expDays);
var expString = ((expires==null) ? "": (";expires="+expDate.toGMTString()));
var pathString = ((path==null) ? "": (";path="+path));
var domainString = ((domain==null) ? "": (";domain="+domain));
var secureString = ((secure==true) ? ";secure": "");
document.cookie = name + "="+ escape(value) + expString + pathString + domainString + secureString;
}
function getCookie(name)
{
var result = null;
var myCookie = document.cookie + ";";
var searchName = name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1)
{
startOfCookie += searchName.length;
endOfCookie = myCookie.indexOf(";",startOfCookie);
result = unescape(myCookie.substring(startOfCookie,endOfCookie));
}
return result;
}
var $ = function(id) {
return document.getElementById(id);
}
function addLoadEvent(func) {
var oldonload= window.onload;
if(typeof window.onload!='function') {
window.onload=func;
} else {
window.onload=function() {
oldonload();
func();
}
}
}
//////////////////////////////////////////////////////////////
var COOKIE_LAST_REG = 'lastReg';
var COOKIE_LAST_TEXT = 'lastText';
function testReg() {//根据输入的正则表达式进行验证
var regText = $('regArea').value.Trim();//正则表达式内容
setCookie(COOKIE_LAST_REG,regText);
var regCtrl = '';
if (!$('distingush').checked)
{
regCtrl += 'i';
}
if($('mutilChoice').checked) {
regCtrl += 'm';
}
var reg = new RegExp(regText,regCtrl);
var text = $('testText').value;//要验证的文本
setCookie(COOKIE_LAST_TEXT, text);
var resultArea = $('resultArea');
if($('showMatch').checked) {
var result = text.match(reg);
if (result == null) {
resultArea.innerHTML = "<span class='wrong'>匹配失败</span>";
} else {
var html = '<span class="right">匹配到的子选项:</span>';
html += '<ul>';
for(var i=1,len=result.length;i<len;i++) {
html += '<li>' + result[i] + '</li>';
}
html += '</ul>'
resultArea.innerHTML = html;
}
} else {
if (reg.test(text))
{
resultArea.innerHTML = "<span class='right'>匹配成功</span>";
}
else
{
resultArea.innerHTML = "<span class='wrong'>匹配失败</span>";
}
}
return true;
}
function init() {
var lastText = getCookie(COOKIE_LAST_TEXT);
if (lastText) {
$('testText').value = lastText;
}
var lastReg = getCookie(COOKIE_LAST_REG);
if (lastReg) {
$('regArea').value = lastReg;
}
}
addLoadEvent(init);
</script>
</head>
<body>
<div id="container">
<h2>正则表达式测试</h2>
<table>
<tbody><tr>
<td>正则表达式:</td>
<td>
<textarea cols="50" rows="5" value="" id="regArea"></textarea>
<input type="button" value="验证" id="testReg" onclick="testReg();">
</td>
</tr>
<tr>
<td>待检验的文本:</td>
<td><textarea cols="50" rows="5" value="" id="testText"></textarea></td>
</tr>
<tr>
<td></td>
<td style="">
</td>
</tr>
</tbody></table>
<fieldset>
<legend>匹配规则</legend>
<input type="radio" name="upDown" id="distingush" value="0">区分大小写
<input type="radio" name="upDown" value="1" checked="">不区分大小写<br />
<input type="radio" name="mutil" id="mutilChoice" value="1">多行模式
<input type="radio" name="mutil" value="0" checked="">单行模式<br />
</fieldset>
<fieldset>
<legend>结果显示</legend>
<input type="checkbox" name="showMatch" id="showMatch" />显示匹配项
</fieldset>
<div class="margin"></div>
<div id="resultArea"></div>
<div id="debug"></div>
</div>
<div class="margin"></div>
<div id="footer">
<a href="http://git.oschina.net/yunnysunny/jsreg" target="_blank">fork me on oschina</a>
</div>
</body></html>