WEB开发网
开发学院WEB开发Jsp javascript 函数原型 阅读

javascript 函数原型

 2008-01-05 18:36:27 来源:WEB开发网   
核心提示:string .length 汉字为两个字符处理.Tlength <script>/* This following code are designed and writen by Windy_sk <seasonx@163.net> You can use it freely, but u m

string .length 汉字为两个字符处理
.Tlength



<script>
/*
 This following code are designed and writen by Windy_sk <seasonx@163.net>
 You can use it freely, but u must held all the copyright items!
*/

//测字符串实际长度
String.PRototype.Tlength = function(){var arr=this.match(/[^\x00-\xff]/ig);return this.length+(arr==null?0:arr.length);}

//字符串左取
String.prototype.left = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(0,num);if(!mode) return str;var n = str.Tlength() - str.length;num = num - parseInt(n/2);return this.substr(0,num);}

//字符串右取
String.prototype.right = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(this.length-num);if(!mode) return str;var n = str.Tlength() - str.length;num = num - parseInt(n/2);return this.substr(this.length-num);}

//字符串包含
String.prototype.GetCount = function(str,mode){return eval("this.match(/("+str+")/g"+(mode?"i":"")+").length");}

//字符串去除两端空字符
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, "");}

document.write("'aa啦啦aa'.Tlength() = " + "aa啦啦aa".Tlength());
document.write("<br>");
document.write("'aabbcc'.Tlength() = " + "aabbcc".Tlength());
document.write("<br>");
document.write("'aa啦啦aa'.left(4) = " + "aa啦啦aa".left(4));
document.write("<br>");
document.write("'aa啦啦aa'.left(4,true) = " + "aa啦啦aa".left(4,true));
document.write("<br>");
document.write("'aa啦啦aa'.right(4) = " + "aa啦啦aa".right(4));
document.write("<br>");
document.write("'aa啦啦aa'.right(4,true) = " + "aa啦啦aa".right(4,true));
document.write("<br>");
document.write("'abAcae'.GetCount('a') = " + "abAcae".GetCount("a"));
document.write("<br>");
document.write("'abAcae'.GetCount('a',true) = " + "abAcae".GetCount("a",true));
document.write("<br>");
document.write("'  abc  de   '.Ltrim() = " + "  abc  de   ".Ltrim());
document.write("<br>");
document.write("'  abc  de   '.Rtrim() = " + "  abc  de   ".Rtrim());
document.write("<br>");
document.write("'  abc  de   '.Trim() = " + "  abc  de   ".Trim());
</script>


再来几个函数,getCookie(),getQuery(),DateDiff(),DateAdd(),b2s(),原+改


function getCookie(mainKey,subKey) {
 var reg = new RegEXP("(^ )"+mainKey+"=([^;]*)(;$)");
 var arr = document.cookie.match(reg);
 if (arguments.length == 2) {
 if (arr!=null)
  return key(subKey,arr[2]);
 else

Tags:javascript 函数 原型

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接