Regex for special characters javascript

I'm trying to create a validation for a password field which allows only the a-zA-Z0-9 characters and .!@#$%^&*()_+-=

I can't seem to get the hang of it.

What's the difference when using regex = /a-zA-Z0-9/g and regex = /[a-zA-Z0-9]/ and which chars from .!@#$%^&*()_+-= are needed to be escaped?

What I've tried up to now is:

var regex = /a-zA-Z0-9!@#\$%\^\&*\)\(+=._-/g

but with no success

Regex for special characters javascript

Ankit Tanna

1,7057 gold badges32 silver badges58 bronze badges

asked Sep 15, 2013 at 12:22

3

var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g

Should work

Also may want to have a minimum length i.e. 6 characters

var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]{6,}$/g

answered Sep 15, 2013 at 12:24

Regex for special characters javascript

Ed HealEd Heal

58.1k16 gold badges84 silver badges121 bronze badges

3

a sleaker way to match special chars:

/\W|_/g

\W Matches any character that is not a word character (alphanumeric & underscore).

Underscore is considered a special character so add boolean to either match a special character or _

answered Apr 17, 2018 at 8:57

TITOTITO

7156 silver badges10 bronze badges

3

What's the difference?

/[a-zA-Z0-9]/ is a character class which matches one character that is inside the class. It consists of three ranges.

/a-zA-Z0-9/ does mean the literal sequence of those 9 characters.

Which chars from .!@#$%^&*()_+-= are needed to be escaped?

Inside a character class, only the minus (if not at the end) and the circumflex (if at the beginning). Outside of a charclass, .$^*+() have a special meaning and need to be escaped to match literally.

allows only the a-zA-Z0-9 characters and .!@#$%^&*()_+-=

Put them in a character class then, let them repeat and require to match the whole string with them by anchors:

var regex = /^[a-zA-Z0-9!@#$%\^&*)(+=._-]*$/

answered Sep 15, 2013 at 12:37

BergiBergi

587k132 gold badges915 silver badges1300 bronze badges

You can be specific by testing for not valid characters. This will return true for anything not alphanumeric and space:

var specials = /[^A-Za-z 0-9]/g;
return specials.test(input.val());

answered Jun 21, 2019 at 14:59

Regex for special characters javascript

PersyJackPersyJack

1,3911 gold badge16 silver badges32 bronze badges

1

Complete set of special characters:

/[\!\@\#\$\%\^\&\*\)\(\+\=\.\<\>\{\}\[\]\:\;\'\"\|\~\`\_\-]/g

To answer your question:

var regular_expression = /^[A-Za-z0-9\!\@\#\$\%\^\&\*\)\(+\=\._-]+$/g

answered Aug 7, 2019 at 15:18

Regex for special characters javascript

Shayan AmaniShayan Amani

5,2611 gold badge36 silver badges36 bronze badges

How about this:-

var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,}$/;

It will allow a minimum of 6 characters including numbers, alphabets, and special characters

answered Sep 15, 2013 at 12:28

Regex for special characters javascript

Rahul TripathiRahul Tripathi

163k31 gold badges266 silver badges322 bronze badges

5

There are some issue with above written Regex.

This works perfectly.

^[a-zA-Z\d\-_.,\s]+$

Only allowed special characters are included here and can be extended after comma.

answered Apr 24, 2014 at 9:53

Sanjeev SinghSanjeev Singh

3,8503 gold badges30 silver badges38 bronze badges

This regex works well for me to validate password:

/[ !"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/

This list of special characters (including white space and punctuation) was taken from here: https://www.owasp.org/index.php/Password_special_characters. It was changed a bit, cause backslash ('\') and closing bracket (']') had to be escaped for proper work of the regex. That's why two additional backslash characters were added.

answered Jun 14, 2019 at 14:30

Regex for special characters javascript

1

// Regex for special symbols

var regex_symbols= /[-!$%^&*()_+|~=`{}\[\]:\/;<>?,.@#]/;

answered Jan 21, 2015 at 0:32

Regex for special characters javascript

Regex for minimum 8 char, one alpha, one numeric and one special char:

/^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/

Armel

2,7816 gold badges20 silver badges35 bronze badges

answered Apr 23, 2019 at 9:16

Regex for special characters javascript

sam rubensam ruben

3672 silver badges6 bronze badges

this is the actual regex only match:

/[-!$%^&*()_+|~=`{}[:;<>?,.@#\]]/g

answered May 24, 2018 at 3:50

chris_rchris_r

1,7241 gold badge19 silver badges22 bronze badges

1

You can use this to find and replace any special characters like in Worpress's slug

const regex = /[`~!@#$%^&*()-_+{}[\]\\|,.//?;':"]/g
let slug = label.replace(regex, '')

answered Feb 10, 2021 at 16:11

Regex for special characters javascript

function nameInput(limitField)
{
//LimitFile here is a text input and this function is passed to the text 
onInput
var inputString = limitField.value;
// here we capture all illegal chars by adding a ^ inside the class,
// And overwrite them with "".
var newStr = inputString.replace(/[^a-zA-Z-\-\']/g, "");
limitField.value = newStr;
}

This function only allows alphabets, both lower case and upper case and - and ' characters. May help you build yours.

answered Jun 13, 2020 at 23:42

2

Not the answer you're looking for? Browse other questions tagged javascript regex or ask your own question.

What is the regex for special characters?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).

How do I allow special characters in JavaScript?

/[a-zA-Z0-9]/ is a character class which matches one character that is inside the class. It consists of three ranges. /a-zA-Z0-9/ does mean the literal sequence of those 9 characters. Which chars from .!

How do I check if a string contains special characters?

To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.

Is a special character in JavaScript?

They all start with a backslash character (\), and are often called escape characters. The following escape characters are allowed in JavaScript. ... Special characters..