Regex to compare strings with Umlaut and non-Umlaut variations

后端 未结 7 1812
刺人心
刺人心 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: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()

提交回复
热议问题