2011-03-01 36 views
3

例如,如果一個單詞是「用戶」,替換是「utilisateurs」,是否有某種功能可以讓我將替換字符串轉換爲與原始單詞相同的首都/非首都級別?如何在做字符串替換時複製案例模式?

這是使用jQuery,textnodes,精確和部分字符串匹配的主要語言翻譯的最後一部分。

現在我只想確保最終的和專業的觸摸,是確保翻譯/替換的單詞的情況下,匹配原始單詞/短語的情況。

我不知道如何將其作爲替換調用應用。

我想這是作爲一個功能

function CopyCase(original,new) { 

    // copy the case of the original to the new 
} 

只是不知道該怎麼做。

+0

此主題不回答問題,但可能會給一些有用的信息,但仍然http://stackoverflow.com/questions/2332811/capitalize-words-in-string – Orbit 2011-03-01 18:27:44

+0

我需要噸他作爲一個函數,2個參數,原始的,新的,然後如何使新詞與第一個詞具有相同的大小寫。 – crosenblum 2011-03-01 18:29:47

+0

所以你想'用戶'轉化爲'Utilisateurs' ...你想'UseRs'轉化爲'UtiLisateurs'或其他?你想如何處理'USERS'?大寫整個翻譯的字符串? – 2011-03-01 18:52:43

回答

1

這裏是將處理的情況下沒有內部資本化的功能:

function caseWord(word, upper) { 
    return String.prototype[ upper ? "toUpperCase" : "toLowerCase"].apply(word[0]) + word.slice(1); 
} 
function caseReplace(s, fromWord, toWord) { 
    return s.replace(caseWord(fromWord), caseWord(toWord)) 
      .replace(caseWord(fromWord, true), caseWord(toWord, true)); 
} 

console.log(caseReplace("The users have joined the Users union.", "users", "utilisateurs")) 
console.log(caseReplace("The lovers have joined the Lovers union.", "lovers", "amants")) 

// The utilisateurs have joined the Utilisateurs union. 
// The amants have joined the Amants union. 
+0

這可以轉換爲函數嗎? – crosenblum 2011-03-01 18:29:15

+0

問題在於我有一長串短語和單詞,每個單詞的capitlization不同。所以我無法預先知道哪個角色會提前被限制/不能提供上限。 – crosenblum 2011-03-01 18:36:55

+0

這不是您指定的簽名(雖然我正在寫它),但可能會有用。 – harpo 2011-03-01 18:47:21

0

String.replace方法接受回調函數作爲其第二個參數。在回調中,您可以在返回替換字符串之前執行所需的任何特殊處理。

var translations = { 
    users: "utilisateurs", 
    apples: "pommes", 
    ... 
}; 

// You could generate the regular expression from the object above 
// or if the number of words is high, not use regular expressions at all. 
s = s.replace(/users|apples|oranges/gi, function (match) { 
    return matchCase(translations[match], match); 
}); 

function getCase(ch) { 
    return ch == ch.toUpperCase() 
     ? "Upper" 
     : "Lower"; 
} 

function convertCase(s, case) { 
    return s["to" + case + "Case"](); 
} 

function matchCase(s, target) { 
    return convertCase(s[0], getCase(target[0])) 
     + convertCase(s.substr(1), getCase(target[1])); 
} 
+0

這會適用於多個單詞或只是一個單詞嗎? – crosenblum 2011-03-01 18:37:40

+0

多個單詞。但正如我所說,如果你有太多的話,使用正則表達式是一種矯枉過正。我會將字符串拆分爲單詞數組,遍歷數組並進行項目的翻譯/大小寫匹配,然後將數組連接回字符串。 – 2011-03-01 18:53:37

0
function matchCase (word1, word2) { 
    // 1) Words starting with two capital letters are assumed to be all caps. 
    // 2) Words starting with two lower case letters are assumed to be all lower case. 
    // 3) Words starting with an upper case followed by a lowercase letter are 
    //  all lower case except the first letter. 

    return word1[0] === word1[0].toLowerCase() ? word2.toLowerCase() : 
      word1[1] === word1[1].toLowerCase() ? word2[0].toUpperCase() + word2.slice (1).toLowerCase() : 
      word2.toUpperCase(); 
} 

matchCase ('Word', 'mot') 
"Mot" 
matchCase ('WOrd', 'mot') 
"MOT" 
matchCase ('word', 'mot') 
"mot" 
matchCase ('wOrD', 'mot') 
"mot" 
matchCase ('woRD', 'mot') 
"mot" 

編輯以改變測試較低的情況下,現在應該是語言無關的(如果底層toLowerCase功能)