Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for diacritics

I'm looking to implement the solution provided in this answer but it's not working. The code in this jsFiddle have looks like this:

function Start() {

    $('#TheBox').keyup(function () {

        var TheInput = $('#TheBox').val();      
        var TheCleanInput = TheInput.replace(/([.\p{L}])/g, '');

        $('#TheBox').val(TheCleanInput);
    });
}

$(Start);

Basically, I'm looking to allow letters such as é è ô as well as numbers. What do I need to change to make the regex filter work?

like image 814
frenchie Avatar asked May 13 '15 21:05

frenchie


People also ask

How do you denote special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is u00C0?

\u00C0 - \u00FF are letters with accents on them, though that isn't all of them. And "°" is just the degree character. However, "./" should probably be "\." to permit period characters.

What is this regex?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

As Casimir et Hippolyte stated in comments, Javascript does not support \p{L} unicode character class.

You can create your own character class:

[a-zA-Z0-9À-ž]

Demo

If you want to allow those characters but replace characters outside those ranges, negate the character classes:

[^a-zA-Z0-9À-ž]

Demo

Or as pointed out in comments:

[A-zÀ-ÖØ-öø-įĴ-őŔ-žǍ-ǰǴ-ǵǸ-țȞ-ȟȤ-ȳɃɆ-ɏḀ-ẞƀ-ƓƗ-ƚƝ-ơƤ-ƥƫ-ưƲ-ƶẠ-ỿ]
like image 179
dawg Avatar answered Oct 03 '22 00:10

dawg