Use locale characters in regular expressions with javascript

javascript, locale, regex

Solution

According to Wikipedia, Spanish alphabet consists of:

- English alphabet: `A-Z`, `a-z`

- N with diacritic tilde: `ñ` and `Ñ`

- Accented characters: `á`, `é`, `í`, `ó`, `ú`, `ü` (and their corresponding uppercase character)

Since there are 2 ways to specify characters with diacritical marks:

- Single glyph: `á`

- With combining mark: `á` (`"a\u0341"`)

You will need to at least take care of such cases. Thankfully, Spanish only has at most 1 diacritical mark on the characters.

In Unicode, there are also characters that decomposes to English alphabet `A-Z` or `a-z`. Since JavaScript's RegExp has poor support for Unicode and they are rarely used anyway, I ignore those cases.

Therefore, to correctly match a Spanish alphabet (single glyph and combining mark):

[aeiouAEIOU]\u0341|[uU]\u0308|[nN]\u0303|[a-zA-ZáéíóúüÁÉÍÓÚÜñÑ]

(Note that `i` flag is not effective on non-US-ASCII characters).

Back to the problem of matching a word. This depends on your definition of a "word character".

Let's say a "word" (Spanish) consists of Spanish alphabet, and digits `0-9`:

(?:[aeiouAEIOU]\u0341|[uU]\u0308|[nN]\u0303|[a-zA-ZáéíóúüÁÉÍÓÚÜñÑ0-9])+

Test code:

'gracias señor señor'.match(/(?:[aeiouAEIOU]\u0341|[uU]\u0308|[nN]\u0303|[a-zA-ZáéíóúüÁÉÍÓÚÜñÑ0-9])+/g).forEach(function(v){console.log(v + " " + v.length)});

Output (matched word and length):

gracias 7
señor 5
señor 6

Problem

I guess it's easier to explain with an example: ``` 'gracias senor'.match(/\w+/g) ["gracias", "senor"] ``` But if I use any non english character: ``` 'gracias señor'.match(/\w+/g) ["gracias", "se", "or"] ``` Is there some way to take into account characters like ñ, á é, etc..

Original source