JS regular expression must contain numbers, letters, special characters

Time:2019-11-13

Let’s introduce that JS regular expressions must contain numbers, letters and special characters

JS regular expression requirements:

1. It must contain numbers, English letters, special symbols and be greater than or equal to 8 digits

2. Special symbols include: ~! @ × $%^&*

The regular expression is as follows:


/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[[email protected]#$%^&*])[\[email protected]#$%^&*]{8,}$/

Interpretation:

Here we mainly use forward looking in JS regular table? =, for example

/\D /. Exec (‘abc2abc ‘) match result is 2, interpretation: match a number

/\D (? = a) /. Exec (‘abc2abc ‘) matches 2, meaning: matches a number, but the number must be followed by the letter a

/\D (? = a) /. Exec (‘abc2bc ‘) match result is null

It can be seen from this that the matching result of? = does not contain the characters it matches, but the string to be matched must conform to the corresponding rules

When there are multiple forward looking rules, they are parallel, not serial, for example

/\d(?=a)(?=b)/.exec( 'abc2abc' ) If you want to match a number, it must be followed by the letters A and B

/\d(?=a)(?=.b)/.exec( 'abc2abc' ),Of course, this is to demonstrate its rules. In fact, it can be written in this way/\d(?=ab)/.exec( 'abc2abc' )

about/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[[email protected]#$%^&*])[\[email protected]#$%^&*]{8,}$/

In fact, the whole regular representation can be divided into three parts:


/^(?=.*\d)[\[email protected]#$%^&*]{8,}$/  
/^(?=.*[a-zA-Z])[\[email protected]#$%^&*]{8,}$/
/^(?=.*[[email protected]#$%^&*])[\[email protected]#$%^&*]{8,}$/

The string to be matched must conform to these three parts at the same time

Knowledge point expansion:

JS regular expression matches special characters except Chinese characters, letters, numbers, commas, periods (used to regulate the input content)


/[^\u4e00-\u9fa5a-zA-Z\d,\.,。]+/ 

Where \ u4e00 – \ u9fa5 matches Chinese characters, a-za-z matches letters, D matches numbers,. Match commas and periods, ^ for non, + for as many matches as possible.

summary

The above-mentioned JS regular expression introduced by Xiaobian to you must contain numbers, letters and special characters. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!