Home  >  Article  >  Web Front-end  >  Detailed explanation of writing simple regular expressions

Detailed explanation of writing simple regular expressions

零下一度
零下一度Original
2017-07-03 14:17:371419browse

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

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

In order to solve the above-mentioned need to add a variable to the regular expression, we can only use the instance creation method

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

Literal method and instance creation method are in regular expressions The difference?

#1. Everything that appears in the literal method is metacharacters, so variable values ​​cannot be spliced, but the instance creation method is possible.

2. Just write \d directly in the literal, but in the example it needs to be translated into \\d

Exercise rules:

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 regularity of the email address ( Short version)

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

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

## 3 , Chinese standard real name 2-4 Chinese characters

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

4. ID 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 is the detailed content of Detailed explanation of writing simple regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn