Javascript String replaceCallback(p,callback)

Description

Javascript String replaceCallback(p,callback)

String.prototype.replaceCallback = function(p,callback){
    var data = this.match(p);
    if( typeof data != "object" || data == null)
        return;/*from w ww  . j a  v  a2 s. c o m*/

    console.log(typeof data);
    if( typeof data.length == "undefined")
        return;
    var len = data.length;

    var str = this;

    for( var i=0; i<len; i++ ){
        var ns = callback(data[i]);
        str = str.replace(data[i],ns);
    }
    return str;
};



PreviousNext

Related