Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to accept alphanumeric and some special character in Javascript? [closed]

I have a Javascript regex like this:

/^[\x00-\x7F]*$/ 

I want to modify this regex so that it accept all capital and non-capital alphabets, all the numbers and some special characters: - , _, @, ., /, #, &, +.

How can I do this?

like image 313
pynovice Avatar asked Jul 03 '13 04:07

pynovice


People also ask

How do I allow only special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).


2 Answers

use:

/^[ A-Za-z0-9_@./#&+-]*$/ 

You can also use the character class \w to replace A-Za-z0-9_

like image 50
Alex Gittemeier Avatar answered Sep 22 '22 06:09

Alex Gittemeier


I forgot to mention. This should also accept whitespace.

You could use:

/^[-@.\/#&+\w\s]*$/ 

Note how this makes use of the character classes \w and \s.

EDIT:- Added \ to escape /

like image 30
NPE Avatar answered Sep 22 '22 06:09

NPE