javascript - Use JS regexp to check if certain chars are in string -


I am working on a JS function to generate password strings, in this case lowercase, as uppercase, 0-9 and Punctuation I have put together a base string, such as:

  function genpwd (azlc, azuc, num, pun, len) {var chars = ""; If (azlc) character + = "abcdefghijklmnopqrstuvwxyz"; If (Ezuk) Characters + = "ABCDEFGHKJLMNOPQRTUVX"; If (number) character + = "012345678901234567890123"; If (pun) chars + = "! @ #% & Amp; () =? + -_.: ;; * {} [] [] ^ $ /";   

Then I select the characters in a new string and give it to the loop through the original string (given length of password) and give it as output password.

 for  (i = 0; i & lt; len; i ++) {nextChar = chars.charAt (Math.For (Math.Random (* * charsN)); Password + = Next charge;} Return password;}   

This is an easy way to create a random string, but does not guarantee that at least one letter output string from each "four group" .

I have seen some other examples of how to use regexps, but it can not be understood how to work It is to modify that I am thinking of solving it in the "simplest" way, maybe it is using a regexpe - something like this:

  if (password.magic_regexp_stuff) {return password ;} Else {password = genpwd (azlc, azuc, num, pun, len);}   
  1. Am I on the right track?
  2. Can I help with regexp?

    Update:

    Okay, after all some valuable input from you, I ended up with MV.

    Thank you (thanks man!) By the CR suggestion was posted by PE, so I'm pretty sure nobrokered will have any other problem about "problem of data" (there is no other word for this). Can anyone confirm this? :)

    I had to modify the parameters of the function and had to approach for the set of characters used. Any suggestions on improvement? passwd (lane, ezlasi, ezuk, number, pans) {var len = parseInt (len), ar = [], checker = [], passwd = '', good = true , Number, number 2, set = 0; If (! LAN? Lan; 4) LAN = 12; If (! AZLC & amp; Azuke & amp; No & amp; Panning) {azlc = 1; Azuk = 1; Num = 1; Water = 1; } If (azlc) {ar.push ("abcdefghijklmnopqrstuvwxyz"); Checker.push (0); Set ++; } If (Azuk) {ar.push ("ABCDEFGHKLMNOPQRTUVWXYZ"); Checker.push (0); Set ++; } If (num) {ar.push ("0123456789"); Checker.push (0); Set ++; } If (pun) {ar.push ("! @ #% & Amp; () =? + -_. :: :: * [] [] ^ $ /"); Checker.push (0); Set ++; } (Var i = 0; i & lt; len; i ++) {num = rand (0, set); NUM2 = RAND (0, AR [NUM]. Length); Password + = ar [number] [NUM2]; Checker [NUM] = 1; } For (var i = 0; i

    Many thanks to everyone for your help, it is appreciated!

    I do not think regular expressions for the job are the right tools. Your efforts are always theoretically loop (Although not very likely) because there is no guarantee that no password ever matches a regular expression.

    I have an easy way to make sure that each group contains a character explicitly, so it should be at least 4 characters long, assuming your password, I will recommend the following pseudo code :

      chars = charFrom (azlc) + charFrom (azuc) + charFrom (num) + CharFrom do the following, length_off_powword - 4 times: character + = charform (eslc + ezuk + Numerator) character = shuffle (character)   

    implementation of charFrom () and reshuffle () are left as an exercise for the reader.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -