views:

435

answers:

6

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 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...

+2  A: 

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.

Rubens Farias
+1  A: 

You can use pipe as an or in a group for each matching like this (ä|ae).

unholysampler
+1  A: 

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

Kimvais
Braces are pythonic? :P At least use non-capturing groups, but this still fails for character classes (`[äö]`).
Roger Pate
+3  A: 

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.

McPherrinM
those are helpful links - thank you.
davek
+4  A: 

something like

tr = {"ä":"ae", "ü":"ue", "ö":"oe", "ß":"ss" }

ersetzeUmlauts = function(s) {
    return s.replace(/[äöüß]/g, function($0) { return tr[$0] })
}

vergleichen = function(a, b) {
    return ersetzeUmlauts(a) == ersetzeUmlauts(b)
}

alert(vergleichen("grüße", "gruesse"))

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

not quite elegant, but works

stereofrog
that looks very handy - I'll give that a try. Thank you!
davek
perfect - it works and I'd also say it's elegant!
davek
A: 

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.

Volker Rose