String.prototype.rTrim=function(){ //去右空格
var re_r=/([.\w]*)[ ]+$/
return this.replace(re_r,"$1")
}
String.prototype.lTrim=function(){ //去左空格
var re_l=/^[ ]+(.*)/
return this.replace(re_l,"$1")
}
String.prototype.trim=function(){ //去左右空格
return this.lTrim().rTrim()
}
JavaScript将字符串转换成日期的方法:
var s = "2009-06-22 09:41:30";
var d = new Date(Date.parse(s.replace(/-/g, "/"))
new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new Date(ms);
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
http://luke.breuer.com/tutorial/
Javascript实现进制转换:
(parseInt(要转数字,数字进制)).toString(目标进制)例如,把10进制的30转为16进制,就是:
(parseInt(30,10)).toString(16)
或是直接: (30).toString(16) 就OK了,因为默认为10进制
应用:
因为(16777215 == ffffff in decimal),所以生成16进制颜色可以这么做:
'#'+Math.floor(Math.random()*16777215).toString(16);
二者并存的情况下,在数值运算中,优先调用了valueOf,字符串运算中,优先调用了toString。
var obj = {};
obj.valueOf = function()
{
return 10;
}
obj.toString = function()
{
return "return value";
}
var result = obj + 1; //var result = obj.valueOf() + 1;
alert(result);
alert(obj); //alert(obj.toString());
变量转换:
var myVar = "3.14159",
str = ""+ myVar,// to string
int = ~~myVar, // to integer
float = 1*myVar, // to float
bool = !!myVar, /* to boolean - any string with length and any number except 0 are true */
array = [myVar]; // to array
不用try catch创建XMLHttpRequest对象
if ( typeof XMLHttpRequest == 'undefined' )
XMLHttpRequest = function() {
// Internet Explorer uses an ActiveXObject to create a new XMLHttpRequest object
// IE 5 uses a different XMLHTTP object from IE 6
return new ActiveXObject(navigator.userAgent.indexOf('MSIE 5') >= 0 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP' );
};
//Create the request object
var xml = new XMLHttpRequest;
function isChinese(temp) {
var re = /[^\u4e00-\u9fa5]/;
if(re.test(temp)) {
return false
}
return true
}
Comparing escape(), encodeURI(), andencodeURIComponent():
http://xkr.us/articles/javascript/encode-compare/
escape() will not encode: @*/+
encodeURI() will not encode: ~!@#$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'"
c();
//a();//runtime error: a is not a function
//b();//runtime error: b is not defined
function c(){};//c will be defined and assigned value when pre-compile
var a = function b(){ //b will be delete, a will be defined when pre-compile, a assigned value when runtime
};
a();
javascript性能测试:
时间格式化
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
An HTML escaper function
function escapeHTML(text) {
var replacements= {"<": "<", ">": ">","&": "&", "\"": """};
return text.replace(/[<>&"]/g, function(character) {
return replacements[character];
});
}