<script>
/**
* @From: http://www.happyshow.org/article.asp?id=135
* @From: http://www.blueidea.com/tech/web/2003/1402.asp
* @From: http://www.cnscn.org
*/
/**
* 具有prototype属性的类型
*
* 数组变量(Array)
* 逻辑变量(Boolean)
* 日期变量(Date)
* 结构变量(Function)
* 数值变量(Number)
* 对象变量(Object)
* 字符串变量(String)
*/
/**
* 去掉字符串首尾的空格
*/
String.prototype.cnsTrim = function(){
var reg=/(^\s*)|(\s*$)/g;
return this.replace(reg, "");
}
/**
* 判断是否合法email
*/
String.prototype.cnsValidEmail = function() {
var reg=/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
return reg.test(this);
}
/**
* 判断是否数字字符串
*/
String.prototype.cnsIsDigit = function() {
var reg=/^([+-])?\d*\.?\d*$/;
return reg.test(this);
}
/**
* 判断是否数字
*/
Number.prototype.cnsIsDigit = function() {
var reg=/^([+-])?\d*\.?\d*$/;
return reg.test(this);
}
/**
* 兼容汉字取字符串字节长
*/
String.prototype.cnsLength = 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.cnsLength() - str.length;
num = num - parseInt(n/2);
return this.substr(0,num);
}
/**
* 取%或千分比等
*/
Number.prototype.toFixed = function(n) {
with(Math) return round(Number(this)*pow(10,n))/pow(10,n)
}
/**
* 取出数组元素
*/
Array.prototype.pop = function() {
var lastElement = this[this.length-1];
this.length = Math.max(this.length-1,0);
return lastElement;
}
/**
* 把元素加入数组
*/
Array.prototype.push = function(new_element){
this[this.length]=new_element;
return this.length;
}
/**
* 把多个元素都加入数组
*/
Array.prototype.push = function() {
for(var i=0;i<arguments.length;i++) {
this[this.length]=arguments;
}
return this.length;
}
/**
* 删除一个元素
*/
Array.prototype.shift = function() {
var firstElement = this[0];
this.reverse();
this.pop();
this.reverse();
return firstElement;
}
Array.prototype.unshift = function() {
var arr = new Array();
for (var i=0;i<arguments.length;arr=arguments[i++]);
arr = arr.concat(this);
this.length = 0;
for (i=0;i<arr.length;this=arr[i++]);
}
/**
* 对数组进行切片
*/
Array.prototype.splice = function() {
var start = arguments[0];
var deleteCount = start+arguments[1];
var deleteItem = this.slice(start,deleteCount);
var beforeItem = this.slice(0,start);
var afterItem = this.slice(deleteCount);
this.length=beforeItem.length;
var i;
for (i=2;i<arguments.length;this[this.length]=arguments[i++]);
for (i=0;i<afterItem.length;this[this.length]=afterItem[i++]);
return deleteItem;
}
/**
* 过滤字符串里面的html标记,类似于 innerText
*/
String.prototype.stripTags = function() {
return this.replace(/<\/?[^>]+>/gi, '');
}
/**
* 过滤掉字符串里面的js
*/
String.prototype.stripScripts = function() {
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
}
/**
* 与 stripScript 相反,取出script标签里面的代码,不包括 <script>标记本身
*/
String.prototype.extractScripts = function() {
var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
return (this.match(matchAll) || []).map(function(scriptTag) {
return (scriptTag.match(matchOne) || ['', ''])[1];
});
}
/**
* 运行脚本
*/
String.prototype.evalScripts = function() {
return this.extractScripts().map(eval);
}
/**
* 类似 escape,转换 < > 为 < >
*/
String.prototype.escapeHTML = function() {
var div = document.createElement('div');
var text = document.createTextNode(this);
div.appendChild(text);
return div.innerHTML;
}
/**
* 和上面相反
*/
String.prototype.unescapeHTML = function() {
var div = document.createElement('div');
div.innerHTML = this.stripTags();
return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}
/**
* var stringA = "?id=2&type=product";
* stringA.toQueryParams() 将返回 {id="2"; type="product"}
* 这是个object,具有id与type属性。
*/
String.prototype.toQueryParams = function() {
var pairs = this.match(/^\??(.*)$/)[1].split('&');
return pairs.inject({}, function(params, pairString) {
var pair = pairString.split('=');
params[pair[0]] = pair[1];
return params;
});
}
/**
* 把字符串转换为数组,每个字符占一个数组元素
*/
String.prototype.toArray = function(arg) {
return this.split(arg?arg:' ');
}
/**
* 把用减号连接的组合单词如:background-color连接成 backgroundColor
* 调用方法:var ss ="background_color"; ss.camelize();
*/
String.prototype.camelize = function() {
var oStringList = this.split('-');
if (oStringList.length == 1) return oStringList[0];
var camelizedString = this.indexOf('-') == 0
? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
: oStringList[0];
for (var i = 1, len = oStringList.length; i < len; i++) {
var s = oStringList;
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
}
/**
* 转义单引号与斜杠
*/
String.prototype.inspect = function() {
return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
}
//测试
//1)
var str=" xxxxxxxxx ";
alert("'"+str.cnsTrim()+"'");
//2)
var str="cnscn@163.com";
alert(str.cnsValidEmail());
//3)
var str="-456"; //调用String.prototype.cnsIsDigit()
alert(str.cnsIsDigit());
var str=-456; //调用Number.prototype.cnsIsDigit()
alert(str.cnsIsDigit());
var arr = new Array();
arr.push(1);
arr.push(2);
arr.push(3);
alert(arr);
var str=" 兼\"容";
//alert(str.cnsLength());
//alert(str.left(2, true));
alert(str.toArray("'"));
</script>