Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions to verify password strength

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use regular expressions to check password strength, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Preface

Password regular check is used when users register. To write the correct regular expression, you must first define the expression rules.

Option 1 (simple)

Suppose password authentication is defined by the following rules:

A minimum of 6 bits, a maximum of 16 bits {6jin16}

Can contain lowercase capital letters [amurz] and uppercase letters [Amurz]

Can contain numbers [0-9]

Can include underscore [_] and minus sign [-]

According to the above rules, it is easy to define regular literals as follows:

Var pattern = / ^ [\ walled -] {6pc16} $/

Analysis of option 1

Literal quantity /

The literal amount of a regular expression is defined as a character contained between a pair of slashes (/), for example:

Var pattern = / slots /

The above literals match all strings that end with the letter "s".

Character class []

Character classes are formed by placing characters in square brackets. A character class can match any character it contains. Therefore, the regular expression / [abc] / matches any of the letters "a", "b", "c".

Character classes can use hyphens to represent a range of characters. To match Latin lowercase letters, you can use / [amurz] /.

Character class\ w

The character class\ w matches any word consisting of ASCII characters, which is equivalent to [a-zA-Z0-9].

[\ uppercase -] means to match any Latin uppercase and lowercase letters, numbers plus underscores and minus signs.

Repeat {}

Use {} to represent the number of times an element repeats in a regular expression.

The previous term should be matched at least n times, but not more than m times.

{n,} match the previous term n or more times

{n} match the previous term n times

[\ wicked -] {6jue 16} means to match any Latin uppercase and lowercase letters, with numbers followed by underscores and minus signs at least 6 times and up to 16 times.

Matching position

^ matches the beginning of a string or, in multiline retrieval, the beginning of a line

Match the end of a string or, in multiline retrieval, the end of a line

/ ^\ w / matches strings that begin with uppercase and lowercase letters or numbers.

Scenario 1 test

The test results are as follows:

Pattern.test ('123456') = true;pattern.test ('-ifat33') = true;pattern.test ('42du') = false;pattern.test (' du42du42du42du421') = false;pattern.test ('42du42') = false

View the source code

According to the test results, we can see that scheme 1 only makes a simple limit to the password, which can not guarantee the strength of the password and the security of the account.

Option 2 (Security)

Suppose password authentication is defined by the following rules:

A minimum of 6 bits, a maximum of 16 bits {6jin16}

Must contain 1 number

Must contain 2 lowercase letters

Must contain 2 uppercase letters

Must contain 1 special character

According to the above rules, it is easy to define regular literals as follows:

Var pattern = / ^. * (? =. {6jue 16}) (? =. *\ d) (? =. * [Amurz] {2,}) (? =. * [Amurz] {2,}) (? =. * [! @ # $% ^ & *?\ (\)]). * $/

Analysis of option 2

Character class.

Character class. Represents any character except newline characters and other Unicode line Terminators.

Positive advance assertion (? =)

Adding an expression between the symbols "? =" and ")" is an antecedent assertion that the expressions in parentheses must match correctly. For example: / Java (? =\:) / can only match Java followed by a colon.

(? =. * [! @ # $% ^ & *?\ (\)])

The advance assertion indicates that a special character must be included. The 10 special characters in the above expression are the upper key characters of the keyboard 1Pol 2. 0, or you can add other special characters. Note: if the added character has a special meaning in the regular expression, it needs to be escaped with a backslash (\) before the symbol.

Scenario 2 testing

The test results are as follows:

Var pattern = / ^. * (? =. {6jue 16}) (? =. *\ d) (? =. * [Amurz] {2,}) (? =. * [Amurz] {2,}) (? =. * [! @ # $% ^ & *?\ (\)]). * $/; pattern.test ('du42DUDUBY') = true;pattern.test ('duDUdbits') = false;pattern.test ('42dudding') = false;pattern.test ('42DUDUDuring') = false Pattern.test ('42duDUU') = false;pattern.test ('42duU (') = false;pattern.test ('42dUUBY') = false; Thank you for reading this article carefully. I hope the article "how to use regular expressions to check password strength" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report