views:

226

answers:

5

How would I convert ú into u in javascript. I might possibly need it for other non-english characters too.

+2  A: 

MovableType has a function called dirify that does that. Here's a PHP version. These are essentially big lookup tables, so it should be pretty easy to move them across to JavaScript.

In theory you could parse the Unicode tables and follow character references until you hit ASCII, but that might not be practical for you.

Stephen Deken
A: 

A similar question has been asked (but in a .NET context): How do I remove diacritics (accents) from a string in .NET?

I think the simplest solution would be to make a mapping table and just do a lookup for each character. You can build a table of characters with their diacritics to their corresponding "base" character, wrap the lookup into a function and you're good to go.

Zack Mulgrew
A: 

Here's one version of a function that does that in Ruby, that I did. Should be pretty straightforward to convert javascript.

http://snippets.dzone.com/posts/show/2384

Tiago
+3  A: 

I have asked a similar question some time ago: JavaScript equivalent of XPath’s translate()?

I think you can use the outcome.

Tomalak
Looks like we have a winner here.
Zack Mulgrew
A: 

check this Englishizer

function Englishizer(var strIn)
{   
 var strOut
    var strMid
    var n 
    For int (n = 1; n<strIn.Length;n++)
{
        strMid = substring(strIn, n, 1)
        Select Case strMid.charCodeAt(0)
        Case 192 To 197:
            strMid = "A"
        Case 198:
            strMid = "AE"
        Case 199:
            strMid = "C"
        Case 200 To 203:
            strMid = "E"
        Case 204 To 207:
            strMid = "I"
        Case 208:
            strMid = "D"
        Case 209:
            strMid = "N"
        Case 210 To 214, 216:
            strMid = "O"
        Case 215:
            strMid = "x"
        Case 217 To 220:
            strMid = "U"
        Case 221:
            strMid = "Y"
        Case 222, 254:
            strMid = "p"
        Case 223:
            strMid = "B"
        Case 224 To 229:
            strMid = "a"
        Case 230:
            strMid = "ae"
        Case 231:
            strMid = "c"
        Case 232 To 235:
            strMid = "e"
        Case 236 To 239:
            strMid = "i"
        Case 240, 242 To 246, 248:
            strMid = "o"
        Case 241:
            strMid = "n"
        Case 249 To 252:
            strMid = "u"
        Case 253, 255:
            strMid = "y"

        Englishizer = Englishizer + strMid;
}
}
Oscar Cabrero