This question already has an answer here:
这个问题在这里已有答案:
- Efficiently replace all accented characters in a string? 20 answers
有效地替换字符串中的所有重音字符? 20个答案
How would I convert ú into u in javascript. I might possibly need it for other non-english characters too.
如何在javascript中将ú转换为u。我可能也需要它用于其他非英语字符。
5 个解决方案
#1
I have asked a similar question some time ago: JavaScript equivalent of XPath’s translate()?
我前段时间问了一个类似的问题:JavaScript相当于XPath的translate()?
I think you can use the outcome.
我想你可以使用结果。
#2
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.
MovableType有一个名为dirify的函数。这是一个PHP版本。这些本质上是大型查找表,因此将它们移动到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.
从理论上讲,您可以解析Unicode表并跟踪字符引用,直到您点击ASCII,但这可能对您不切实际。
#3
A similar question has been asked (but in a .NET context): How do I remove diacritics (accents) from a string in .NET?
已经提出了类似的问题(但在.NET上下文中):如何从.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.
我认为最简单的解决方案是制作一个映射表,然后对每个字符进行查找。你可以用它们的变音符号构建一个字符表到相应的“基础”字符,将查找包装成一个函数,你就可以了。
#4
Here's one version of a function that does that in Ruby, that I did. Should be pretty straightforward to convert javascript.
这是在Ruby中执行该功能的一个版本,我做过。转换javascript应该非常简单。
#5
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;
}
}
#1
I have asked a similar question some time ago: JavaScript equivalent of XPath’s translate()?
我前段时间问了一个类似的问题:JavaScript相当于XPath的translate()?
I think you can use the outcome.
我想你可以使用结果。
#2
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.
MovableType有一个名为dirify的函数。这是一个PHP版本。这些本质上是大型查找表,因此将它们移动到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.
从理论上讲,您可以解析Unicode表并跟踪字符引用,直到您点击ASCII,但这可能对您不切实际。
#3
A similar question has been asked (but in a .NET context): How do I remove diacritics (accents) from a string in .NET?
已经提出了类似的问题(但在.NET上下文中):如何从.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.
我认为最简单的解决方案是制作一个映射表,然后对每个字符进行查找。你可以用它们的变音符号构建一个字符表到相应的“基础”字符,将查找包装成一个函数,你就可以了。
#4
Here's one version of a function that does that in Ruby, that I did. Should be pretty straightforward to convert javascript.
这是在Ruby中执行该功能的一个版本,我做过。转换javascript应该非常简单。
#5
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;
}
}