Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: allow everything but some selected characters [duplicate]

I would like to validate a textarea and I just don't get regex (It took me the day and a bunch of tutorials to figure it out).

Basically I would like to be able to allow everything (line breaks and chariots included), but the characters that could be malicious( those which would lead to a security breach). As there are very few characters that are not allowed, I assume that it would make more sense to create a black list than a white one.

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

I'm using javascript and jquery.

I tried this but it doesn't work (it's awful, I know..):

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

Thank you.

like image 316
Baylock Avatar asked Aug 23 '12 18:08

Baylock


1 Answers

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

/[^.?!]/

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

like image 138
murgatroid99 Avatar answered Oct 14 '22 20:10

murgatroid99