Skip to content
the poor coder

diacritic case insensitive regex

Poor Man
Published
Reading time
2 min read
On this page

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:

JavaScript

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:

JavaScript

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC