Javascript regex to allow alphanumeric and reject multiple special characters

I need JavaScript Regular Expression to allow alphanumeric and reject & ,; , '

I can do a regular expression to allow a specific character, for example ^[a-zA-Z0-9 @$]*$ , it allows alphanumeric, space, @ and $ .

Is there any example where I can say allow all special character, but reject & ,; and '

+4
source share
3 answers

If the goal is to allow everything except the & ; and ' , you can simply ignore them:

 [^&;']* 

All alphanumeric characters and any other special character must allow this.

If your list of valid / rejected characters is more complex, it may be easier to write a regular expression specifying a valid, for example, source pattern ^[a-zA-Z0-9 @$]*$ . It all depends on your needs and / or if they will change (I personally would prefer your original regular expression if it does not change much).

+2
source

The format [^ ] is the opposite of [ ] . To do what you want, try the following:

 ^[a-zA-Z0-9 @$]*|[^&;\`]* 
+1
source

/ ^ [a-zA-Z0-9 @ $] | [^ &; `," '% !. ()] $ /

0
source

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


All Articles