Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Array of Regular Expressions Javascript

I want to create a function that compares a password against some commonly idiotic ones, so that the user can't pick one of these, but the function I have written so far, when put between script tags, causes no javascript to be recognized (by Firebug). I assume the array creation is at fault.

function unacceptable(pwd){     var unforgivable = [     /password/gi, /*g matches any occurance of sequence, i checks case insensitive*/     /12345678/g,     /8675309/g,     /[a-z]{8,}/gi,     /qwerty/gi,     /asdfg/gi,     /qazwsx/gi,     /zxcvb/gi,     /letmein/gi,     /trustno1/gi,     /omnicloud/gi,     /monkey/gi];     for (var i=0; i<unforgivable.length; i++)         if(pwd.match(unforgivable[i])) return true;     return false; }  
like image 367
Chris Avatar asked Nov 21 '11 03:11

Chris


People also ask

How can you create an array in JavaScript?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

How do you match expressions in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

How do I convert a string to an array in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

You don't need the loop to test every word as you can put them all into one regular expression (separated by the | character) and let the regex engine look for any of them all at once. You could do that like this:

function unacceptable(pwd){     var unforgivable = [         "password",         "12345678",         "8675309",         "[a-z]{8,}",         "qwerty",         "asdfg",         "qazwsx",         "zxcvb",         "letmein",         "trustno1",         "omnicloud",         "monkey"     ];     var re = new RegExp(unforgivable.join("|"), "i");     return re.test(pwd); } 

Working demo here: http://jsfiddle.net/jfriend00/cyVbC/

P.S. You don't have to put all the words into an array. You could just predeclare the entire regex, but I thought putting them in the array like this made for more readable code that was easier to maintain.

It could also be this:

var unforgivable = /password|12345678|8675309|[a-z]{8,}|qwerty|asdfg|qazwsx|zxcvb|letmein|trustno1|omnicloud|monkey/i;  function unacceptable(pwd){     return unforgivable.test(pwd); } 
like image 186
jfriend00 Avatar answered Oct 13 '22 09:10

jfriend00