当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 扩展String功能方法

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 扩展String功能方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 185 ::
收藏到网摘: n/a

一直好忙,没有时间写属于自己的东西,但是看着一天天不更新心情也不是个滋味,只有从网上收罗一些比较好的东东贴上。
/*** 删除首尾空格 ***/
String.prototype.Trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
/*** 统计指定字符出现的次数 ***/
String.prototype.Occurs = function(ch) {
// var re = eval("/[^"+ch+"]/g");
// return this.replace(re, "").length;
return this.split(ch).length-1;
}
/*** 检查是否由数字组成 ***/
String.prototype.isDigit = function() {
var s = this.Trim();
return (s.replace(/\d/g, "").length == 0);
}
/*** 检查是否由数字字母和下划线组成 ***/
String.prototype.isAlpha = function() {
return (this.replace(/\w/g, "").length == 0);
}
/*** 检查是否为数 ***/
String.prototype.isNumber = function() {
var s = this.Trim();
return (s.search(/^[+-]?[0-9.]*$/) >= 0);
}
/*** 返回字节数 ***/
String.prototype.lenb = function() {
return this.replace(/[^\x00-\xff]/g,"**").length;
}
/*** 检查是否包含汉字 ***/
String.prototype.isInChinese = function() {
return (this.length != this.replace(/[^\x00-\xff]/g,"**").length);
}
/*** 简单的email检查 ***/
String.prototype.isEmail = function() {
 var strr;
var mail = this;
 var re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
 re.exec(mail);
 if(RegExp.$3!="" && RegExp.$3!="." && RegExp.$2!=".")
strr = RegExp.$1+RegExp.$2+RegExp.$3;
 else
  if(RegExp.$2!="" && RegExp.$2!=".")
strr = RegExp.$1+RegExp.$2;
  else
 strr = RegExp.$1;
 return (strr==mail);
}
/*** 简单的日期检查,成功返回日期对象 ***/
String.prototype.isDate = function() {
var p;
var re1 = /(\d{4})[年./-](\d{1,2})[月./-](\d{1,2})[日]?$/;
var re2 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{2})[年]?$/;
var re3 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{4})[年]?$/;
if(re1.test(this)) {
p = re1.exec(this);
return new Date(p[1],p[2],p[3]);
}
if(re2.test(this)) {
p = re2.exec(this);
return new Date(p[3],p[1],p[2]);
}
if(re3.test(this)) {
p = re3.exec(this);
return new Date(p[3],p[1],p[2]);
}
return false;
}
/*** 检查是否有列表中的字符字符 ***/
String.prototype.isInList = function(list) {
var re = eval("/["+list+"]/");
return re.test(this);
}