[转帖]Mootools源码分析-10 -- String

原帖地址:http://space.flash8.net/space/?uid-18713-action-viewspace-itemid-401801

原作者:我佛山人

 

//对字符串类型的扩展实现
String.implement({

    
//Regex实例方法test的快捷方式
    test: function(regex, params)    {
        
return ((typeof regex == 'string'? new RegExp(regex, params) : regex).test(this);
    },

    
/*测试字符串中是否包含指定子串
    配合separator参数可以测试类似逗号分隔序列中是否包含指定的项
    例如:'jpg,gif,png'.contains(file_ext_name, ',')
    
*/
    contains: 
function(string, separator)    {
        
return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1;
    },

    
//过滤字符串中的前导及后导空白
    trim: function()    {
        
return this.replace(/^\s+|\s+$/g, '');
    },

    
//将连续空白替换为一个空格
    clean: function()    {
        
return this.replace(/\s+/g, ' ').trim();
    },

    
//将字符串骆驼化(各单词首字母大写),与hyphenate、capitalize配合,通常用于变量/属性名
    camelCase: function()    {
        
return this.replace(/-\D/g, function(match)    {
            
return match.charAt(1).toUpperCase();
        });
    },

    
//骆驼字符连接化
    hyphenate: function()    {
        
return this.replace(/[A-Z]/g, function(match)    {
            
return ('-' + match.charAt(0).toLowerCase());
        });
    },

    
//使首字母大写
    capitalize: function()    {
        
return this.replace(/\b[a-z]/g, function(match)    {
            
return match.toUpperCase();
        });
    },

    
//转义正则字符串
    escapeRegExp: function()    {
        
return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
    },

    
//parseInt的快捷方式
    toInt: function(base)    {
        
return parseInt(this, base || 10);
    },

    
//toFloat的快捷方式
    toFloat: function()    {
        
return parseFloat(this);
    },

    
//十六进制颜色表示转为RGB表示
    hexToRgb: function(array)    {
        
var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
        
return (hex) ? hex.slice(1).hexToRgb(array) : null;
    },

    
//十六进制颜色表示转为RGB表示
    rgbToHex: function(array)    {
        
var rgb = this.match(/\d{1,3}/g);
        
return (rgb) ? rgb.rgbToHex(array) : null;
    },

    
//RGB颜色表示转为十六进制表示
    stripscrīpts: function(option)    {
        
var scrīpts = '';
        
var text = this.replace(/<scrīpt[^>]*>([\s\S]*?)<\/scrīpt>/gi, function()    {
            scrīpts += arguments[1+ '\n';
            
return '';
        });
        
if (option === true)    $exec(scrīpts);
        
else if ($type(option) == 'function')    option(scrīpts, text);
        
return text;
    },

    
/*
    类似format的一种替换方式,根据提供的regexp匹配对的子串,用相应的object对象属性来替换
    例如:
    var student = new Student('John', 'male', 17);
    alert('{name} is a {sex}, and {age} years old.'.substitute(student));
    将显示
    John is a male, and 17 years old.
    是不是很优雅?
    
*/
    substitute: 
function(object, regexp)    {
        
return this.replace(regexp || (/\\?\{([^}]+)\}/g), function(match, name)    {
            
if (match.charAt(0== '\\')    return match.slice(1);
            
return (object[name] != undefined) ? object[name] : '';
        });
    }

});

 

posted @ 2009-10-28 09:40  webgis松鼠  阅读(133)  评论(0编辑  收藏  举报