Regex to enter a description form

the text box is part of my form. The user has to write a little text, and I want to check this text. I am currently using the following regex:

/^[0-9a-zA-ZäöüÄÖÜ_\-']+$/

Although I mentioned äöüÄÖÜ in regular expression, it treats all words with äöü .. as invalid. In addition, he does not accept empty spaces.

Any ideas on improving regex?

+3
source share
4 answers

Use a regex that supports Unicode:

/[\pL\pN_\-]+/
+5
source

the PCRE u modifier allows utf-8 to be used. You also lack space from the regex, and you can condensate it a bit:

/^[0-9a-zäöü\- ]+$/ui

, "i" .

.

+2

-, , äöüÄÖÜ . PHP, , , . , , . , , , .

-, ,

/^[0-9a-z A-ZäöüÄÖÜ_\-']+$/ // note space after a-z

. , . \w , a-zA-ZäöüÄÖÜ

+1

You can simply use \wto indicate all the characters of the word (letters, numbers, etc.). Thus, the regular expression will be equal. /^[\w_\-' ]+$/
What text do you think is "valid" in the user?

+1
source

Source: https://habr.com/ru/post/1777298/


All Articles