Regex to compare strings with Umlaut and non-Umlaut variations

后端 未结 7 1712
刺人心
刺人心 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:46

    I have another way : ( purpose : sorting arrays )

    function umlaut(str) {
     return str
      .replace(/Â|À|Å|Ã/g, "A")
      .replace(/â|à|å|ã/g, "a")
      .replace(/Ä/g, "AE")
      .replace(/ä/g, "ae")
      .replace(/Ç/g, "C")
      .replace(/ç/g, "c")
      .replace(/É|Ê|È|Ë/g, "E")
      .replace(/é|ê|è|ë/g, "e")
      .replace(/Ó|Ô|Ò|Õ|Ø/g, "O")
      .replace(/ó|ô|ò|õ/g, "o")
      .replace(/Ö/g, "OE")
      .replace(/ö/g, "oe")
      .replace(/Š/g, "S")
      .replace(/š/g, "s")
      .replace(/ß/g, "ss")
      .replace(/Ú|Û|Ù/g, "U")
      .replace(/ú|û|ù/g, "u")
      .replace(/Ü/g, "UE")
      .replace(/ü/g, "ue")
      .replace(/Ý|Ÿ/g, "Y")
      .replace(/ý|ÿ/g, "y")
      .replace(/Ž/g, "Z")
      .replace(/ž/, "z"); 
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-13 10:54

    Regular expressions aren't quite powerful enough to do this properly, though you could hack it into almost working with them.

    What you want is called Unicode Normalization. A Normalized string is one converted to a common form so you can compare them. You tagged your post "javascript", however, Javascript doesn't have a built in standard library to do this, and I am not aware of one offhand. Most server-side languages do have one, though. For example, the Normalizer Class in PHP. Python and Perl have equivalents, as do Microsoft stuff, I'm sure.

    Check out the wikipedia article on Unicode Equivalence for more information.

    0 讨论(0)
  • 2021-01-13 10:56

    One way is to process your regexp 'input' so that it replaces for example 'ä' with (ae|ä)' - not hardcode the mappings to your regexps. I am completely ignorant to javascript (ok, i know document.write() but that's about it) - but here is the same in pseudo code;

    instead of doing

    regexp_match("Grüße|Gruesse",somestring)
    

    You should do something like:

    mappings = (("ä","ae"),("ö","oe"),("ü","ue"))
    def my_regexp_match(regexp,input) {
        for key,value in mappings {
             new_regexp = regexp.replace(key,"("+key+"|"+value+")")
        }
        regexp_match(new_regexp,input)
    }
    my_regexp_match("Grüße",somestring)
    

    Sorry for being so "pythonic" - I do not know if you have re.compile() -like structure in javascript, but if you do - you should do the for -loop when compiling the matcher, not in my_regexp_match()

    0 讨论(0)
  • 2021-01-13 11:00

    In addition to stereofrogs answer:

    tr = {"\u00e4":"ae", "\u00fc":"ue", "\u00f6":"oe", "\u00df":"ss" }
    
    ersetzeUmlauts = function(s) {
        return s.replace(/[\u00e4|\u00fc|\u00f6|\u00df]/g, function($0) { return tr[$0] })
    }
    

    I was dealing with Umlauts in an Aptana/Eclipse script and the normal characters ('ä' etc.) didn't do the trick for me.

    0 讨论(0)
  • 2021-01-13 11:06

    Regex isn't better technology to solve this problem.

    You should consider to create a dictionary to store your Umlaut character as key and non-Umlaut as value; Then you can to iterate over yourdictionary, check if it exists on your string and take appropriate action.

    0 讨论(0)
提交回复
热议问题