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

The difference between the ways of creating js regular expressions

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "differences in the way js regular expressions are created". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the literal way, all the contents wrapped between us / / are metacharacters, some of which have special meaning, and most of them are ordinary metacharacters that represent their own meaning.

Var name = 'wo'; var reg = / ^\ d + "+ name+"\ dfarmers /

In order to solve the above requirement of adding a variable to the rule, we can only use the method of instance creation

Var reg = new RegExp ("^\\ d +" + name+ "\\ dlegs $", "g")

What is the difference between literal quantity and instance creation in the rule?

1. Everything that appears in the literal quantity mode is metacharacter, so it is impossible to concatenate the values of variables, but it is possible to create instances.

2. Write\ d directly in the literal quantity, but it needs to be translated\\ d in the example.

Practice the rules:

1. Age 18-65 / / Age 18-19 20-59 60-65

Var reg = / ^ (1 [8jue 9] | [2jue 5]\ d | 6 [0rem 5]) $/

2. Verify the regularity of the mailbox (simplified version)

The rules on the left side of the mailbox: numbers, letters, underscores,.,-

Var reg = / ^ [\ w. -] + @ [0-9a-zA-Z] + (\. [a-zA-Z] {2jue 4}) {1jue 2} $/

3. Chinese standard real names with 2-4 characters

Var reg = / ^ [\ u4e00 -\ u9fa5] {2pr 4} $/

4. ID card number

Var reg = / ^\ d {17} (\ d | x) $/ var reg = / ^\ (d {2}) (\ d {4}) (\ d {4}) (\ d {2}) (\ d) (\ d | X) $/ "differences in the way js regular expressions are created" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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