I found a regular expression that works for a particular case: an input that can receive all kinds of accents and also a hyphen, and prevents any other signs inside the input.
However, I don't really understand why it is defined like this. I don't want to copy and paste something that I don't understand why it works for me. I don't know if it applies to a question, but I ask it anyway.
/^[a-zA-ZÀ-ÖØ-öø-ÿ]+$/
The regular expression:
/^[a-zA-ZÀ-ÖØ-öø-ÿ]+$/
is composed of the following:that is, the regular expression matches any string that has the characters between the square brackets [] and has a length of 1 or greater.
The regular expression uses as base the ascii characters https://www.ascii-code.com/
I also recommend you to use : https://regexr.com/ or https://regex101.com/ to evaluate your expressions
Complementing FelipeM's answer , the character class
[a-zA-ZÀ-ÖØ-öø-ÿ]
matches 1 character, any within those 5 ranges.And those 5 ranges cover all the letters (alphabets) of the 2 most used Unicode blocks :
\u0000
a\u007F
), and\u0080
a\u00FF
).A-Z
anda-z
in the Basic Latin block: 1* Characters from \u0000 to \u001F ( control codes C0 ), which are not printable, are not displayed.
* Characters from \u0080 to \u009F ( control codes C1 ) are not displayed, and are also not printable.
À-Ö
,Ø-ö
andø-ÿ
in the Latino-1 Supplementary block: 2I mean,
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
.Grades:
The middle hyphen (
-
) is not included. Hyphens in the character class only build ranges. If you wanted to include it, you would add it escaped like\-
inside the square brackets.- Another option is to add the hyphen, without escaping it, at the beginning or end of the class, where they never form a range.
Spaces and other punctuation marks are also not included.
Does not include all Latin letters with diacritics. Some very rare characters from the Extended Latin-A (
\u0100
a\u017F
) or Extended Latin-B (\u0180
a\u024F
) blocks are not in the class.Examples:
ĩ
,Ō
,ũ
,ŷ
,ƒ
,ǎ
,ȩ
,Ɇ
, etc.Letters from the Additional Extended Latin Block , nor from the Greek , Cyrillic , Armenian , Hebrew , or Arabic alphabets , among many others , are obviously not included either .
The Match Unicode Block Range page generates a character class from the Unicode blocks that are selected.
If you only want Spanish letters, the class is
[A-Za-zÁÉÍÓÚÜÑáéíóúüñ]
o, which is the same, ignoring upper and lower case:While there is no other way in JavaScript (which has the worst regex implementation), other languages allow more friendly ways to match letters.
In theoretical computation and theory of formal languages, a regular expression, also known as regex, regexp1 or rational expression,23 is a sequence of characters that forms a search pattern, mainly used for searching for patterns of character strings. or substitution operations.
In your example: