Javascript match function for special characters - javascript

I am working on this code and using "match" function to detect strength of password. how can I detect if string has special characters in it?
if(password.match(/[a-z]+/)) score++;
if(password.match(/[A-Z]+/)) score++;
if(password.match(/[0-9]+/)) score++;

If you mean !##$% and ë as special character you can use:
/[^a-zA-Z ]+/
The ^ means if it is not something like a-z or A-Z or a space.
And if you mean only things like !#$&$ use:
/\W+/
\w matches word characters, \W matching not word characters.

You'll have to whitelist them individually, like so:
if(password.match(/[`~!##\$%\^&\*\(\)\-=_+\\\[\]{}/\?,\.\<\> ...
and so on. Note that you'll have to escape regex control characters with a \.
While less elegant than /[^A-Za-z0-9]+/, this will avoid internationalization issues (e.g., will not automatically whitelist Far Eastern Language characters such as Chinese or Japanese).

you can always negate the character class:
if(password.match(/[^a-z\d]+/i)) {
// password contains characters that are *not*
// a-z, A-Z or 0-9
}
However, I'd suggest using a ready-made script. With the code above, you could just type a bunch of spaces, and get a better score.

Just do what you did above, but create a group for !##$%^&*() etc. Just be sure to escape characters that have meaning in regex, like ^ and ( etc....
EDIT -- I just found this which lists characters that have meaning in regex.

if(password.match(/[^\w\s]/)) score++;
This will match anything that is not alphanumeric or blank space. If whitespaces should match too, just use /[^\w]/.

As it look from your regex, you are calling everything except for alphanumeric a special character. If that is the case, simply do.
if(password.match(/[\W]/)) {
// Contains special character.
}
Anyhow how why don't you combine those three regex into one.
if(password.match(/[\w]+/gi)) {
// Do your stuff.
}

/[^a-zA-Z0-9 ]+/
This will accept only special characters and will not accept a to z & A to Z 0 to 9 digits

Related

regex to strictly check alphanumeric and special character

To check alphanumeric with special characters
var regex = /^[a-zA-Z0-9_$#.]{8,15}$/;
return regex.test(pass);
But, above regex returns true even I pass following combination
asghlkyudet
78346709tr
jkdg7683786
But, I want that, it must have alphanumeric and special character otherwise it must return false for any case. Ex:
fg56_fg$
Sghdfi#90
You can replace a-zA-Z0-9_ with \w, and using two anchored look-aheads - one for a special and one for a non-special, the briefest way to express it is:
/^(?=.*[_$#.])(?=.*[^_$#.])[\w$#.]{8,15}$/
Use look-ahead to check that the string has at least one alphanumeric character and at least one special character:
/^(?=.*[a-zA-Z0-9])(?=.*[_$#.])[a-zA-Z0-9_$#.]{8,15}$/
By the way, the set of special characters is too small. Even consider the set of ASCII characters, this is not even all the special characters.
The dollar sign is a reserved character for Regexes. You need to escape it.
var regex = /^[a-zA-Z0-9_/$#.]{8,15}$/;

I want to ignore square brackets when using javascript regex [duplicate]

This question already has answers here:
Why is this regex allowing a caret?
(3 answers)
Closed 1 year ago.
I am using javascript regex to do some data validation and specify the characters that i want to accept (I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed). My code is:
var value = userInput;
var pattern = /[^A-z0-9 "!&,'\-]/;
if(patt.test(value) == true) then do something
It works fine and excludes the letters that I don't want the user to enter except the square bracket and the caret symbols. From all the javascript regex tutorials that i have read they are special characters - the brackets meaning any character between them and the caret in this instance meaning any character not in between the square brackets. I have searched here and on google for an explanation as to why these characters are also accepted but can't find an explanation.
So can anyone help, why does my input accept the square brackets and the caret?
The reason is that you are using A-z rather than A-Za-z. The ascii range between Z (0x5a) and a (0x61) includes the square brackets, the caret, backquote, and underscore.
Your regex is not in line with what you said:
I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed
If you want to accept only those characters, you need to remove the caret:
var pattern = /^[A-Za-z0-9 "!&,'\\-]+$/;
Notes:
A-z also includesthe characters: [\]^_`.
Use A-Za-z or use the i modifier to match only alphabets:
var pattern = /^[a-z0-9 "!&,'\\-]+$/i;
\- is only the character -, because the backslash will act as special character for escaping. Use \\ to allow a backslash.
^ and $ are anchors, used to match the beginning and end of the string. This ensures that the whole string is matched against the regex.
+ is used after the character class to match more than one character.
If you mean that you want to match characters other than the ones you accept and are using this to prevent the user from entering 'forbidden' characters, then the first note above describes your issue. Use A-Za-z instead of A-z (the second note is also relevant).
I'm not sure what you want but I don't think your current regexp does what you think it does:
It tries to find one character is not A-z0-9 "!&,'\- (^ means not).
Also, I'm not even sure what A-z matches. It's either a-z or A-Z.
So your current regexp matches strings like "." and "Hi." but not "Hi"
Try this: var pattern = /[^\w"!&,'\\-]/;
Note: \w also includes _, so if you want to avoid that then try
var pattern = /[^a-z0-9"!&,'\\-]/i;
I think the issue with your regex is that A-z is being understood as all characters between 0x41 (65) and 0x7A (122), which included the characters []^_` that are between A-Z and a-z. (Z is 0x5A (90) and a is 0x61 (97), which means the preceding characters take up 0x5B thru 0x60).

Regex with special chars

Hi I have this regex.
/^[\w]|[åäöæøÅÄÖÆØ]$/
"tå" is ok but "åå" is not. Why is that? How can I make it accept words starting with åäöæøÅÄÖÆØ?
Note that the \w (and \W, \b, and \B) are English-centric. \w just means [A-Za-z0-9_], where A-Z means only the 26 English letters. Other letters are not considered part of a "word" by JavaScript's built-in character classes.
You'll need to build a character class including all of the letters you want to treat as word characters (then use the negated version of that wherever you "non-word character").
But that's not the only problem. Your regular expression says:
Match one English word character at the beginning of the string, or match one of this list of characters at the end of the string.
The | operator is fairly greedy, in this case it treats ^[\w] and [åäöæøÅÄÖÆØ]$ as the alternatives. I don't get the impression that's what you wanted.
"tå" is ok but "åå" is not.
I guess it depends on what you mean by "ok". Both match the expression:
console.log("tå".match(/^[\w]|[åäöæøÅÄÖÆØ]$/)); // ["t", index: 0, input: "tå"]
console.log("åå".match(/^[\w]|[åäöæøÅÄÖÆØ]$/)); // ["å", index: 1, input: "åå"]
"tå" matches because it matches the ^[\w] alternative. "åå" matches because it matches the [åäöæøÅÄÖÆØ]$ alternative.
How can I make it accept words starting with åäöæøÅÄÖÆØ?
If the goal is to accept only strings containing exactly one word, where "word" includes digits and the underscore (since \w does), then:
/^[A-Za-z0-9_åäöæøÅÄÖÆØ]+$/
Why do you think it fails? I would not put the \w in square brackets but various systems seem to allow that and both the following match the text being tested.
Javascript
var test = 'åå';
if (test.match(/^[\w]|[åäöæøÅÄÖÆØ]$/)) { alert("Match"); }
PHP
echo(preg_match("/^[\w]|[åäöæøÅÄÖÆØ]$/","åå")."</br>");
What are you trying to achieve here?

Regex Expression To Match Special Characters And Numbers

I'm looking for a regexp to see if a string contains any special characters, numbers or anything else but letters.
For example I have a string "This is a 5 string #". Now I would need a regexp to see if this string contains any special characters like # or numbers like 5.
I'm not familiar with using regexp approaches.
you can use .test() method
if ("This is a 5 string #".test(/[^a-z]/i)) { ... }
this will find if some symbols different from a-z and A-Z are inside the string. Note also that this regexp won't accept accented letters. in that case you will need a more refined regexp like
/[^a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF]/
see a unicode table to choose what symbols are acceptable in your string
http://unicode.org/charts/
The basics you want is something like /^[a-zA-Z]+$/
this will tell you if your string as any charachters of a to z upper and lowercase.
There are tons off resources online to learn more about regex, a good resource is http://www.regular-expressions.info/reference.html

Need help: RegEx for a password containing numbers and special chars

I need help with a RegEx for a password. The password must contain at least one special char (like "§$&/!) AND a number.
E.g. a password like "EdfA433&" must be valid whereas "aASEas§ö" not as it contains not a number.
I have the following RegEx so far:
^(?=.*[0-9])(?=.*[a-zA-Z]).{3,}$
But this one is obviously checking only for a number. Can anyone help?
You're better off just using multiple more simple regular expressions: any code checking anything like this won't be performance sensitive, and the additional complexity of maintenance given a more complex regexp probably isn't justifiable.
So, what I'd go for:
var valid = foo.match(/[0-9]/) && foo.match(/["§$&/!]/);
I wonder if you really want to define special characters like that: Does é count as a special character? Does ~ count as a special character?
^(?=.*\d)(?=.*\W).{3,}$
checks for at least one digit (\d) and one non-alphanumeric character (\W). \W is the inverse of \w which matches digits, letters and the underscore.
If you want to include the underscore in the list of "special characters", use
^(?=.*\d)(?=.*[\W_]).{3,}$
I would divide function that checks if password is "hard" into some parts and in each part I would check one condition. You can see some complicated regex on Daily WTF with password reset: http://thedailywtf.com/Articles/The-Password-Reset-Facade.aspx

Categories