问题描述
任何人都可以使用JavaScript正则表达式来帮助我,我可以使用它们来比较相同的字符串,并考虑它们的非Umlaut版本.
Can anyone help me with a javascript regular expression that I can use to compare strings that are the same, taking into acccount their non-Umlaut-ed versions.
例如,德语中的Grüße
也可以写为 Gruesse
.这两个字符串应视为相同.映射(暂时忽略大小写)为:
for example, in German the word Grüße
can also be written Gruesse
. These two strings are to be considered identical. The mappings (ignoring casings for the moment) are:
- ä= ae
- ü= ue
- ö= oe
- ß= ss
由于没有太多联结"要考虑,我可以为每个变体进行替换,但是我想知道是否有更优雅的方式,尤其是因为将来可能需要扩展此用例以包括例如斯堪的纳维亚字符...
As there are not many "couplets" to consider I could do a replace for each variation, but I'm wondering if there is a more elegant way, especially as this use case might need to be extended in future to include e.g. Scandanavian characters...
推荐答案
类似
tr = {"ä":"ae", "ü":"ue", "ö":"oe", "ß":"ss" }
replaceUmlauts = function(s) {
return s.replace(/[äöüß]/g, function($0) { return tr[$0] })
}
compare = function(a, b) {
return replaceUmlauts(a) == replaceUmlauts(b)
}
alert(compare("grüße", "gruesse"))
您可以通过在"tr"中添加更多条目来轻松扩展此范围
you can easily extends this by adding more entries to "tr"
不是很优雅,但是可以工作
not quite elegant, but works
这篇关于正则表达式比较带有Umlaut和非Umlaut变体的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!