In programming, we often encounter situations where user input needs to be verified. Regular expressions are usually used for verification. The test() method is used in js to verify the regular expressions. So how to use the test() method?
How to use the test() method for regular verification
The test() method is used to detect whether a string Match a certain pattern
Recommendation:js tutorial
The regular verification of the invoice header in a business recently encountered is as follows:
console.log(/^[a-zA-Z\u4e00-\u9fa5\s()()<>《》\(\)\-]+$/.test("中国xxxx公司七五0试验场"))
When the user enters the company name above, an error occurs and the verification fails. This is because according to Chinese company regulations, Arabic numerals are not allowed in company names, so when 0 appears, the regular verification fails.
Company names shall not contain the following words:
1. Those that are harmful to the interests of the country and society;
2. Those that may cause deception or misunderstanding to the public;
3. Name of foreign country (region), name of international organization;
4. Name of political party, name of party, government and military organs, name of mass organization, name of social group and troop number;
5 , foreign characters, Chinese pinyin letters, Arabic numerals;
6, other laws and administrative regulations prohibiting.
For more JavaScript tutorials, please pay attention to the PHP Chinese websiteFront-end video tutorials.
The above is the detailed content of How to use the test() method for regular verification. For more information, please follow other related articles on the PHP Chinese website!