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?
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).
\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.
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.
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À-ÖØ-öø-įĴ-őŔ-žǍ-ǰǴ-ǵǸ-țȞ-ȟȤ-ȳɃɆ-ɏḀ-ẞƀ-ƓƗ-ƚƝ-ơƤ-ƥƫ-ưƲ-ƶẠ-ỿ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With