2015-02-24 24 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

相關問題