Differences between regular expression creation methods and simple regular expression compiling methods (js learning summary), regular expression js

Source: Internet
Author: User

Differences between regular expression creation methods and simple regular expression compiling methods (js learning summary), regular expression js

In the literal mode, all the content we wrap between them are metacharacters, and some have special meanings. Most of them are common metacharacters that represent their meanings.

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

To solve the above requirement of adding a variable to the regular expression, we can only use the instance creation method.

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

What is the difference between the literal mode and the instance Creation Mode in regular expressions?

1. Everything that appears in the literal mode is metacharacters. Therefore, you cannot splice variable values, but you can create an instance.

2. Write \ d directly in the literal, and translate it into \ d in the instance.

Exercise regular expression:

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

 var reg = /^(1[8,9] | [2,5]\d | 6[0,5])$/

2. Verify the email address Regular Expression (Lite version)

Rules on the left side of the mailbox: numbers, letters, underscores, periods, and ,.,-

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

3. The Chinese standard real name contains 2-4 Chinese Characters

var reg = /^[\u4e00-\u9fa5]{2,4}$/

4. ID card number

 var reg = /^\d{17}(\d | x)$/  var reg = /^\(d{2})(\d{4})(\d{4})(\d{2})(\d{2})(\d{2})(\d)(\d | X)$/

The above section describes the differences between creating regular expressions and writing simple regular expressions (js learning summary). I hope this will help you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.