首页 > 程序开发 > web前端 > JavaScript >

js函数扩展

2011-08-08

扩展去除字符串后面的字符或者空格//去掉字符串两端的字符String.prototype.trim = function() { var charlist = arguments[0] || ''; if(charlist == '') return this.replace(/...

扩展去除字符串后面的字符或者空格

//去掉字符串两端的字符

String.prototype.trim = function() {

var charlist = arguments[0] || ''; if(charlist == '') return this.replace(/(^\s*)|(\s*$)/g, "");

else return this.replace(eval('/(^\\'+charlist+'*)|(\\'+charlist+'*$)/g'), "");

}

/*** 默认去掉字符串左边所有空白

*/

String.prototype.ltrim = function(charlist) {

var charlist = charlist || '';

if(charlist == ''){

return this.replace(/(^\s*)/g, "");

}else{

var reg = '/(^\\'+charlist+'*)/g';

return this.replace(eval(reg), "");

}

}

/*** 默认去掉字符串右边所有空白*/

String.prototype.rtrim = function(charlist) {

var charlist = charlist || '';

if(charlist == ''){

return this.replace(/(\s*$)/g, "");

}else{

var reg = '/(\\'+charlist+'*$)/g';

return this.replace(eval(reg), "");

}

相关文章
最新文章
热点推荐