Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an alphanumeric Regex for all languages?

I had this problem today:

This regex matches only English: [a-zA-Z0-9].

If I need support for any language in this world, what regex should I write?

like image 420
tawfekov Avatar asked Jul 14 '11 11:07

tawfekov


1 Answers

Alphabet/Letter: \p{L}

Number: \p{N}

So for alphnum match for all languages, you can use: [\p{L}\p{N}]+

I was looking for a way to replace all non-alphanum chars for all languages with a space in JS and ended up using the following way to do it:

const regexForNonAlphaNum = new RegExp(/[^\p{L}\p{N}]+/ug);
someText.replace(regexForNonAlphaNum, " ");

Here as it is JS, we need to add u at end to make the regex unicode aware and g stands for global as I wanted match all instances and not just a single instance.

References:

https://www.linkedin.com/pulse/regex-one-pattern-rule-them-all-find-bring-darkness-bind-carranza/?trackingId=U6tRte%2BzTAG6O4AA3CrFmA%3D%3D

https://www.regular-expressions.info/unicode.html

like image 146
amit Avatar answered Sep 28 '22 05:09

amit