Regex: allow all but some selected characters

I would like to check the text box and I just don't get the regex (it took me a day and a bunch of tutorials to figure this out).

Basically, I would like to be able to resolve everything (including line breaks and chariots), but characters that could be malicious (those that could lead to a security breach). Since there are very few characters that are not allowed, I guess it would be wiser to create a blacklist than white.

My question is: what is the standard "everything except" in Regex?

I am using javascript and jquery.

I tried this, but it does not work (this is terrible, I know ..):

var messageReg = /^[a-zA-Z0-9éèêëùüàâöïç\"\/\%\(\).'?!,@$#§-_ \n\r]+$/; 

Thanks.

+6
source share
3 answers

As Esailija mentioned, this will not do anything for real security.

The code you mentioned is an almost negative set, since the mentioned murgatroid99, ^ is in parentheses. Thus, the regular expression will match what is not in this list. But it looks like you really want to remove these characters, so your regular expression does not need to be undone.

Your code should look like this:

 str.replace(/[a-zA-Z0-9éèêëùüàâöïç\"\/\%\(\).'?!,@$#-_ \n\r]/g, ""); 

That says, remove all characters in my regex.

However, this means that you do not want to save a-zA-Z0-9 , are you sure you want to disable them?

Also, chrome is not like in regular expressions, you should use \x along with the hexadecimal code for the character

+1
source

If you want to exclude a character set (for example, some punctuation characters), you must use the ^ operator at the beginning of the character set in the regular expression, for example

 /[^.?!]/ 

This matches any character that is not . , ? or ! .

+16
source

You can use ^ as the first character inside the brackets [] to nullify what's in it:

 /^[^abc]*$/ 

This means: "from start to finish, no a , b or c ."

+6
source

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


All Articles