Regex to compare strings with Umlaut and non-Umlaut variations

后端 未结 7 1784
刺人心
刺人心 2021-01-13 10:34

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.

for e

7条回答
  •  庸人自扰
    2021-01-13 10:51

    something like

    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"))
    

    you can easily extends this by adding more entries to "tr"

    not quite elegant, but works

提交回复
热议问题