<mce:script language=javascript><!--
//全角空格为12288,半角空格为32
//其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
//document.write((65281).toString(16)+"----"+
//(65374).toString(16)+"----"+(12288).toString(16));
String.prototype.dbc2sbc = function ()
{
return this.replace(/[\uff01-\uff5e]/g,
function(a){
return String.fromCharCode(a.charCodeAt(0)-65248);
}).replace(/\u3000/g," ");
}
document.write("ABC 123,我们都是好朋友".dbc2sbc());
// --></mce:script>
这是自定义string类型的方法
这里说明下这个代码
this.replace(/[\uff00-\uff5e]/g),......先看这一半 其实是把全角在65281-65374之间的
换为半角
之后的函数是function返回的字符
在其中的函数也把空格符 也就是全角的空格是\u3000 换成空格
下面 、string 类型可以直接调用
//String.prototype使用
//批量替换,比如:str.ReplaceAll([/a/g,/b/g,/c/g],["aaa","bbb","ccc"])
String.prototype.ReplaceAll=function (A,B) {
var C=this;
for(var i=0;i<A.length;i++) {
C=C.replace(A[i],B[i]);
};
return C;
};
// 去掉字符两端的空白字符
String.prototype.Trim=function () {
return this.replace(/(^[\t\n\r]*)|([\t\n\r]*$)/g,'');
};
// 去掉字符左边的空白字符
String.prototype.LTrim=function () {
return this.replace(/^[\t\n\r]/g,'');
};
// 去掉字符右边的空白字符
String.prototype.RTrim=function () {
return this.replace(/[\t\n\r]*$/g,'');
};
// 返回字符的长度,一个中文算2个
String.prototype.ChineseLength=function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
};
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith=function (A,B) {
var C=this.length;
var D=A.length;
if(D>C)return false;
if(B) {
var E=new RegExp(A+'$','i');
return E.test(this);
}else return (D==0||this.substr(C-D,D)==A);
};
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
};
// 字符串从哪开始多长字符去掉
String.prototype.Remove=function (A,B) {
var s='';
if(A>0)s=this.substring(0,A);
if(A+B<this.length)s+=this.substring(A+B,this.length);
return s;
};
//去掉字符串左边的空格
String.prototype.ltrim = function()
{
return this.replace( /^\s*/,"");
}
//去除字符串右边的空格;
String.prototype.rtrim = function()
{
return this.replace( /\s*$/,"");
}
//去掉字符串中的空格;
function Intrim(s){
return s.replace( /\s/g,"");
}
//去除字符串左右的空格;
String.prototype.trim = function()
{
return (this.rtrim()).ltrim()
}
//判断密码安全级别
String.prototype.checkPassWordLevel = function()
{
var n=0;
if (/\d/.test(this)) n ++; //包含数字
if (/[a-z]/.test(this)) n ++; //包含小写字母
if (/[A-Z]/.test(this)) n ++; //包含大写字母
if (this.length == 6) n=1; //长度小于6位
return n;
}
//判断密码安全级别2 按长度
String.prototype.checkPassWordLevel1 = function()
{
var grade=0;
if (this.length >= 6 && this.length <= 9)
{
grade = 1;
}
if (this.length >= 10 && this.length <= 15)
{
grade = 2;
}
if (this.length >= 16 && this.length <= 20)
{
grade = 3;
}
return grade;
}
//检测是否是正确的Email格式
String.prototype.isEmail = function()
{
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.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.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;
}
}
//是否是汉字
String.prototype.CheckChinese = function()
{
var reg=/^[\u0391-\uFFE5]+$/ ;
// [\u4E00-\u9FA5];
return reg.test(this);
}
//是否是手机号码
String.prototype.IsMobile = function()
{
var reg = /^(13|14|15|18)[0-9]{9}$/;
return reg.test(this);
}