开发者

How to copy case pattern when doing string replace?

开发者 https://www.devze.com 2023-02-14 17:15 出处:网络
F开发者_如何学Cor example if a word is \"Users\" and the replacement is \"utilisateurs\", is there some kind of function that will allow me to conver the replacement string to the same level of capita

F开发者_如何学Cor example if a word is "Users" and the replacement is "utilisateurs", is there some kind of function that will allow me to conver the replacement string to the same level of capitals/non-capitals as the original word?

This is a final part of a major language translation using jquery, textnodes, exact and partial string matching.

Now I just want to make sure as the final and professional touch, is to make sure the case of the translated/replaced words, match the case of the original words/phrases.

I am not sure how I can apply this as a replace call.

I want this to be as a function

function CopyCase(original,new) {

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

Only not sure how to do that.


Here's a function that will handle cases without internal capitalization:

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.


The String.replace method accepts a callback function as its second argument. In that callback, you can perform any special processing you need, before returning a replacement string.

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]));
}


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"

Edited to change the test for lower case which should now be language agnostic (if the underlying toLowerCase function is)

0

精彩评论

暂无评论...
验证码 换一张
取 消