0

I would like to convert the occurence of some special characters with a chainable function, to work something like this:

>>> "Den här ån gör gott".normalizor().toUpperCase();

"DEN HAR AN GOR GOTT"

I'm only interested to convert certain characters:

å >> a
ä >> a   
ö >> o 

Any help to get me in the right direction here would be much appreciated!

Pure javascript or use of any library doesn't matter.

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • 1
    Are you asking about the replacement or the chaining? What does your current code looks like? – bfavaretto Nov 18 '13 at 04:36
  • I'd be happy to be advised about both. I thought of posting my poor attempts, but I don't think it would help anyone in any direction to publish the litter. – Fellow Stranger Nov 18 '13 at 04:40
  • 1
    There is a policy here that says *Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)* – bfavaretto Nov 18 '13 at 04:46
  • 1
    I agree. I thought that a clearly expressed question would somehow be enough and more valuable to the community, but I was wrong. – Fellow Stranger Nov 18 '13 at 04:53
  • 2
    String.prototype.normalizor = function() { return this.replace(/å|ä/g, "a").replace(/ö/g, "o") } – Tesseract Nov 18 '13 at 04:56

3 Answers3

1

normalize seems like a better method name:

"Den här ån gör gott".normalize().toUpperCase();

String.prototype.normalize = function() {
    return this.replace(/[åä]/g, 'a')
               .replace(/[ö]/g, 'o');
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
0

If you want to convert these special characters, you can map the ASCII values of the German(I'm assuming) characters to simple english ones.

For example, here you want to map

ASCII 229(å) to ASCII 97(a)

ASCII 228(ä) to ASCII 97(a)

ASCII 246(ö) to ASCII 111(o)

function normalizor(char) {
  if (char.charCodeAt()===228 || char.charCodeAt()===229) 
    return String.fromCharCode(97);
    //or simply return 'a';
} 
tewathia
  • 6,890
  • 3
  • 22
  • 27
0

Google gave me this solution :

String.prototype.normalizor = function() {
    var s, i, l, diacritics, chars;
    s = this;
    chars = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', 'N', 'n', 'C', 'c'];
    diacritics = [
        /[\300-\306]/g, /[\340-\346]/g, // A, a
        /[\310-\313]/g, /[\350-\353]/g, // E, e
        /[\314-\317]/g, /[\354-\357]/g, // I, i
        /[\322-\330]/g, /[\362-\370]/g, // O, o
        /[\331-\334]/g, /[\371-\374]/g, // U, u
        /[\321]/g, /[\361]/g, // N, n
        /[\307]/g, /[\347]/g, // C, c
    ];
    for (i = 0, l = diacritics.length; i < l; i++) {
        s = s.replace(diacritics[i], chars[i]);
    }
    return s;
}

Same topic on SO : Replacing diacritics in Javascript.

Community
  • 1
  • 1