2015-02-24 41 views
0

我对我的搜索变量进行替换,将已知组转换为组名。如何在仍返回值的同时断开自定义方法链?

我觉得在每一步我都可以进行正则表达式匹配,看看搜索查询中是否还有两个或更多的组成员,如果没有,则中止链。这是我经常使用的功能,如果没有匹配,它有责任跳出过程。

我的实际替换链长度为15长,如果我能在第一个或第二个跳出,看起来是合适的。

所以,我想我会写这样的事情

String.prototype.abortreplace = function (m,r) { 
 
    var toreturn; 
 
    if (this.match(/\b\w\b/g).length > 0) { 
 
     toreturn = this.replace(m,r); 
 
    } else { 
 
     return; 
 
    } 
 
    return toreturn; 
 
} 
 

 
tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p"; 
 
tx2 = tx.abortreplace(/a,b,c/g,"first three letters").abortreplace(/d,e,f/g,"second three letters").abortreplace(/g,h,i/g,"third three letters").abortreplace(/j,k,l/g,"fourth three letters").abortreplace(/m,n,o/g,"fifth three letters").abortreplace(/p,q,r/g,"sixth three letters"); 
 
alert(tx2);

这个字符串的结束,因为工作,因为P的这一特定字符串我有作弊到length > 0。在实践中,长度将是length > 2。在这种情况下,它返回undefined并中断。 我很好奇我怎么能返回字符串,并仍然打破了连锁。(我也试过return false,它返回false而不是undefined)。

String.prototype.abortreplace = function (m,r) { 
 
    var toreturn; 
 
    if (this.match(/\b\w\b/g).length > 2) { 
 
     toreturn = this.replace(m,r); 
 
    } else { 
 
     return; 
 
    } 
 
    return toreturn; 
 
} 
 

 
tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p"; 
 
tx2 = tx.abortreplace(/a,b,c/g,"first three letters").abortreplace(/d,e,f/g,"second three letters").abortreplace(/g,h,i/g,"third three letters").abortreplace(/j,k,l/g,"fourth three letters").abortreplace(/m,n,o/g,"fifth three letters").abortreplace(/p,q,r/g,"sixth three letters"); 
 
alert(tx2);

一个明显的解决方法是简单地return this当条件不匹配,但当然不中断的链的,它只是否定每个连续步骤。

我也知道我会这么的东西大致是这样的:

var groups = ["a,b,c","d,e,f"] 
var gnames = ["first three letters","second three letters"] 
function chainreplace(query,step) { 
    if (this.match(/\b\w\b/g).length > 0) { 
    query = query.replace(groups[step],gnames[step]); 
    if (step < groups.length) { 
     query = chainreplace(query,step+1); 
    } 
    return query; 
    } 
} 
chainreplace("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p",1); 

但我更喜欢一个链接方法如果可能的话,更容易重用(类似,但不完全相同),而无需创建对象的多个阵列

+2

你不能返回一个神奇地让JS忽略表达式其余部分的值。 – 2015-02-24 21:46:36

+1

...除了抛出一个例外,但这可能不是你想要的。你写这个链的方式总是会执行15个方法调用。 – Bergi 2015-02-24 22:08:38

回答

3

与其将自己的方法放在String原型(ugh)上,您可以使用自己的数据结构来实现自己想要的功能。

下面的想法是,有一种类型的对象执行所需的处理并返回可链接的对象,并且当时间到了时,它可以使用相同的接口返回不同的类型,从而使后续链接调用短路:

var replacer = (function() { 
 
    function fixedReplacer(str) { 
 
    var r = { 
 
     abortreplace: function() { 
 
     // this abortreplace just returns the object it was called on 
 
     return r; 
 
     }, 
 
     toString: function() { 
 
     return str; 
 
     } 
 
    }; 
 
    return r; 
 
    } 
 

 
    function replacer(str) { 
 
    return { 
 
     abortreplace: function(m, r) { 
 
     return (str.match(/\b\w\b/g)||[]).length > 2 
 
      ? replacer(str.replace(m, r)) 
 
      : fixedReplacer(str); 
 
     }, 
 
     toString: function() { 
 
     return str; 
 
     } 
 
    }; 
 
    } 
 
    
 
    return replacer; 
 
})(); 
 

 
tx = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p"; 
 
tx2 = replacer(tx) 
 
    .abortreplace(/a,b,c/g, "first three letters") 
 
    .abortreplace(/d,e,f/g, "second three letters") 
 
    .abortreplace(/g,h,i/g, "third three letters") 
 
    .abortreplace(/j,k,l/g, "fourth three letters") 
 
    .abortreplace(/m,n,o/g, "fifth three letters") 
 
    .abortreplace(/p,q,r/g, "sixth three letters") 
 
    .toString(); 
 
console.log(tx2);

当然,这并不妨碍所有15个方法的发生调用(如菲利克斯和BERGI人士指出,这是不可能的,没有抛出异常),但它可以显著降低执行的计算量。

+0

非常感谢您的及时和翔实的答复。 – 2015-02-24 22:28:07

+0

再次感谢你。偶尔,'str.match'返回'NULL',所以长度会引发错误。我在http:// stackoverflow找到了一个很好的答案。com/questions/6715025/regexp-match-length-returns-null-if-not-found并且修改你的回复以防万一它对任何人都有帮助。我希望这是正确的。 – 2015-03-16 20:20:23

相关问题