Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 34243  / 12 Years ago, thu, august 23, 2012, 12:00:00

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éèêëùüàâöïç\/%().'?!,@$#§-_ nr]+$/;


Thank you.


More From » jquery

 Answers
18

As Esailija mentioned, this won't do anything for real security.



The code you mentioned is almost a negated set, as murgatroid99 mentioned, the ^ goes inside the brackets. So the regular expression will match anything that is not in that list. But it looks like you really want to strip out those characters, so your regexp doesn't need to be negated.



Your code should look like:



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


That says, remove all the characters in my regular expression.



However, that is saying you don't want to keep a-zA-Z0-9 are you sure you want to strip those out?



Also, chrome doesn't like § in Regular Expressions, you have to use the x along with the hex code for the character


[#83460] Wednesday, August 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;