diacritic case insensitive regex

Diacritic case insensitive regex

Regular expressions (regex) are a powerful tool for matching patterns in text. However, when working with text that contains diacritics (accented characters) or when matching text without taking case into account, there are some additional considerations to keep in mind.

Diactric case insensitive regex

A diacritic case insensitive regex is a regular expression that matches text regardless of whether it contains diacritics or not and regardless of its case. This can be useful when working with text that may contain a mix of uppercase and lowercase letters and/or diacritics.

One way to create a diacritic case insensitive regex is to use the i flag in combination with the u flag. The i flag makes the regex case insensitive, while the u flag makes it Unicode-aware, allowing it to match diacritics.

Here's an example:


const regex = /hello world/iu;
console.log(regex.test('Hello World')); // true
console.log(regex.test('héllo wórld')); // true
console.log(regex.test('HELLO WORLD')); // true

In this example, the regular expression /hello world/iu matches the string "Hello World" (with different capitalization) and "héllo wórld" (with diacritics).

Other methods

Another way to create a diacritic case insensitive regex is to use character classes to match diacritics explicitly. For example, the character class [\u0300-\u036f] matches all combining diacritical marks. To make the regex case insensitive, you can use the i flag as before.

Here's an example:


const regex = /h[éèêë]llo w[oóòôö]rld/i;
console.log(regex.test('Hello World')); // true
console.log(regex.test('héllo wórld')); // true
console.log(regex.test('HELLO WORLD')); // true

In this example, the regular expression /h[éèêë]llo w[oóòôö]rld/i matches the string "Hello World" (with different capitalization) and "héllo wórld" (with diacritics).

In conclusion, diacritic case insensitive regexes can be useful when working with text that may contain diacritics or when matching text without taking case into account. There are several ways to create such regexes, and the best approach may depend on the specific requirements of your use case.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe