Alon
Alon

Reputation: 3906

javascript regex for special characters

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

Upvotes: 74

Views: 368969

Answers (15)

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20148

If we need to allow only number and symbols (- and .) then we can use the following pattern

const filterParams = {
    allowedCharPattern: '\\d\\-\\.', // declaring regex pattern
    numberParser: text => {
        return text == null ? null : parseFloat(text)
    }
}

Upvotes: 0

jessica caballero
jessica caballero

Reputation: 1

This works for me in React Native:

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

Here's my reference for the list of special characters:

Upvotes: 0

Gayan Sandamal
Gayan Sandamal

Reputation: 337

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

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

Upvotes: 1

Samuel Isirima
Samuel Isirima

Reputation: 29

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.

Upvotes: 0

Shawn Azdam
Shawn Azdam

Reputation: 6190

Complete set of special characters:

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

To answer your question:

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

Upvotes: 11

PersyJack
PersyJack

Reputation: 1974

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());

Upvotes: 16

AlexGongadze
AlexGongadze

Reputation: 389

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.

Upvotes: 4

sam ruben
sam ruben

Reputation: 385

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

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

Upvotes: 3

Anil Singhania
Anil Singhania

Reputation: 845

// Regex for special symbols

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

Upvotes: 5

chris_r
chris_r

Reputation: 2146

this is the actual regex only match:

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

Upvotes: 2

TITO
TITO

Reputation: 855

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 _

Upvotes: 53

Sanjeev Singh
Sanjeev Singh

Reputation: 4066

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.

Upvotes: 5

Bergi
Bergi

Reputation: 665361

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!@#$%\^&*)(+=._-]*$/

Upvotes: 13

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

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

Upvotes: 8

Ed Heal
Ed Heal

Reputation: 60037

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

Upvotes: 76

Related Questions