String对象扩展
JS String对象的一些扩展: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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448/*
******************************************
字符串函数扩充
******************************************
*/
/*
===========================================
//去除左边的空格
===========================================
*/
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
/*
===========================================
//去除右边的空格
===========================================
*/
String.prototype.Rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}
/*
===========================================
//去除前后空格
===========================================
*/
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
/*
===========================================
//得到左边的字符串
===========================================
*/
String.prototype.Left = function(len)
{
if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)
{
len = this.length;
}
}
return this.substr(0,len);
}
/*
===========================================
//得到右边的字符串
===========================================
*/
String.prototype.Right = function(len)
{
if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)
{
len = this.length;
}
}
return this.substring(this.length-len,this.length);
}
/*
===========================================
//得到中间的字符串,注意从0开始
===========================================
*/
String.prototype.Mid = function(start,len)
{
return this.substr(start,len);
}
/*
===========================================
//在字符串里查找另一字符串:位置从0开始
===========================================
*/
String.prototype.InStr = function(str)
{
if(str==null)
{
str = "";
}
return this.indexOf(str);
}
/*
===========================================
//在字符串里反向查找另一字符串:位置0开始
===========================================
*/
String.prototype.InStrRev = function(str)
{
if(str==null)
{
str = "";
}
return this.lastIndexOf(str);
}
/*
===========================================
//计算字符串打印长度
===========================================
*/
String.prototype.LengthW = function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}
/*
===========================================
//是否是正确的IP地址
===========================================
*/
String.prototype.isIP = function()
{
var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
if (reSpaceCheck.test(this))
{
this.match(reSpaceCheck);
if (RegExp.$1 <= 255 && RegExp.$1 >= 0
&& RegExp.$2 <= 255 && RegExp.$2 >= 0
&& RegExp.$3 <= 255 && RegExp.$3 >= 0
&& RegExp.$4 <= 255 && RegExp.$4 >= 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/*
===========================================
//是否是正确的长日期
===========================================
*/
String.prototype.isLongDate = function()
{
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(r==null)
{
return false;
}
var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
/*
===========================================
//是否是正确的短日期
===========================================
*/
String.prototype.isShortDate = function()
{
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)
{
return false;
}
var d = new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
/*
===========================================
//是否是正确的日期
===========================================
*/
String.prototype.isDate = function()
{
return this.isLongDate()||this.isShortDate();
}
/*
===========================================
//是否是手机
===========================================
*/
String.prototype.isMobile = function()
{
return /^0{0,1}13[0-9]{9}$/.test(this);
}
/*
===========================================
//是否是邮件
===========================================
*/
String.prototype.isEmail = function()
{
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}
/*
===========================================
//是否是邮编(中国)
===========================================
*/
String.prototype.isZipCode = function()
{
return /^[\\d]{6}$/.test(this);
}
/*
===========================================
//是否是有汉字
===========================================
*/
String.prototype.existChinese = function()
{
//[\u4E00-\u9FA5]為漢字﹐[\uFE30-\uFFA0]為全角符號
return /^[\x00-\xff]*$/.test(this);
}
/*
===========================================
//是否是合法的文件名/目录名
===========================================
*/
String.prototype.isFileName = function()
{
return !/[\\\/\*\?\|:"<>]/g.test(this);
}
/*
===========================================
//是否是有效链接
===========================================
*/
String.prototype.isUrl = function()
{
return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
}
/*
===========================================
//是否是有效的身份证(中国)
===========================================
*/
String.prototype.isIDCard = function()
{
var iSum=0;
var info="";
var sId = this;
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙 江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖 北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
if(!/^\d{17}(\d|x)$/i.test(sId))
{
return false;
}
sId=sId.replace(/x$/i,"a");
//非法地区
if(aCity[parseInt(sId.substr(0,2))]==null)
{
return false;
}
var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
//非法生日
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
return false;
}
for(var i = 17;i>=0;i--)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
}
if(iSum%11!=1)
{
return false;
}
return true;
}
/*
===========================================
//是否是有效的电话号码(中国)
===========================================
*/
String.prototype.isPhoneCall = function()
{
return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}
/*
===========================================
//是否是数字
===========================================
*/
String.prototype.isNumeric = function(flag)
{
//验证是否是数字
if(isNaN(this))
{
return false;
}
switch(flag)
{
case null: //数字
case "":
return true;
case "+": //正数
return /(^\+?|^\d?)\d*\.?\d+$/.test(this);
case "-": //负数
return /^-\d*\.?\d+$/.test(this);
case "i": //整数
return /(^-?|^\+?|\d)\d+$/.test(this);
case "+i": //正整数
return /(^\d+$)|(^\+?\d+$)/.test(this);
case "-i": //负整数
return /^[-]\d+$/.test(this);
case "f": //浮点数
return /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
case "+f": //正浮点数
return /(^\+?|^\d?)\d*\.\d+$/.test(this);
case "-f": //负浮点数
return /^[-]\d*\.\d$/.test(this);
default: //缺省
return true;
}
}
/*
===========================================
//是否是颜色(#FFFFFF形式)
===========================================
*/
String.prototype.IsColor = function()
{
var temp = this;
if (temp=="") return true;
if (temp.length!=7) return false;
return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
}
/*
===========================================
//转换成全角
===========================================
*/
String.prototype.toCase = function()
{
var tmp = "";
for(var i=0;i<this.length;i++)
{
if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255)
{
tmp += String.fromCharCode(this.charCodeAt(i)+65248);
}
else
{
tmp += String.fromCharCode(this.charCodeAt(i));
}
}
return tmp
}
/*
===========================================
//对字符串进行Html编码
===========================================
*/
String.prototype.toHtmlEncode = function()
{
var str = this;
str=str.replace(/&/g,"&");
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");
str=str.replace(/\'/g,"'");
str=str.replace(/\"/g,""");
str=str.replace(/\n/g,"<br>");
str=str.replace(/\ /g," ");
str=str.replace(/\t/g," ");
return str;
}
/*
===========================================
//转换成日期
===========================================
*/
String.prototype.toDate = function()
{
try
{
return new Date(this.replace(/-/g, "\/"));
}
catch(e)
{
return null;
}
}