Below is a JS regular expression that must contain numbers, letters, and special characters
js regular expression requirements:
1. Must contain numbers, English letters, special symbols and greater than or equal to 8 digits
2. Special symbols include:~!@#$%^&*
The regular expression is as follows:
/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,}$/
explain:
Here we mainly use forward look in js regular tables ?=, for example
/\d/.exec( 'abc2abc' ) The matching result is 2, meaning: match a number
/\d(?=a)/.exec( 'abc2abc' ) The matching result is 2, meaning: match a number, but this number must be followed by the letter a
/\d(?=a)/.exec( 'abc2bc' ) The matching result is null
From this we can see that the ?= match result does not contain the characters it matches, but the string to be matched must comply with its corresponding rules.
When there are multiple forward-looking rules, they are parallel, not serial, e.g.
/\d(?=a)(?=b)/.exec( 'abc2abc' )
The matching result is null. If you want to match a number, you must follow the letters a and b to write this way.
/\d(?=a)(?=.b)/.exec( 'abc2abc' ),
Of course this is to demonstrate its rules, but it can be written like this/\d(?=ab)/.exec( 'abc2abc' )
for/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,}$/
In fact, the entire regular representation can be divided into three parts:
/^(?=.*\d)[\da-zA-Z~!@#$%^&*]{8,}$/ /^(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{8,}$/ /^(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,}$/
The string to be matched must meet these three parts at the same time.
Knowledge point expansion:
js regular expression matches special characters except Chinese characters, letters, numbers, commas, and periods (used to standardize input content)
/[^\u4e00-\u9fa5a-zA-Z\d,\.,。]+/
Where \u4e00-\u9fa5 matches Chinese characters, a-zA-Z matches letters, and \d matches numbers,,.,. Match commas and periods, ^ means non, + means as many matches as possible.
Summarize
The above is the JS regular expression introduced by the editor to you that must contain numbers, letters, and special characters. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!