Home  >  Article  >  Web Front-end  >  Introduction and application of regular metacharacters

Introduction and application of regular metacharacters

零下一度
零下一度Original
2017-07-02 09:27:421794browse

1. Metacharacters:

Each regular expression is composed of metacharacters and modifiers

[Metacharacters] ->Some characters with special meaning between two /

reg = /^\d$/  //只能是一个0-9之间的数字

 1. Characters with special meaning Metacharacter

## \: Escape character, translate the meaning of the following characters

 ^: Start with a certain metacharacter

 $: ends with a metacharacter 

\n: matches a newline character

 .: Any character except \n 

 (): Grouping-> Divide a large regular pattern into several small regular patterns  

 x|y: One of x or y

 [xyz]: One of x, y or z A

## [^xyz]: Any character except three

 [a-z]: Any character between a-z

 [^a-z]: Except any character between a-z

 \d: A character between 0-9 \D Any characters except numbers between 0-9

## \b: A boundary character "w1 w2 w3"

##\w: Any character among numbers, letters, and underscores [0-9a-zA-Z_]

##\s: Matches a whitespace character, a space, a tab character, a form feed character.. .

## 2. Quantifier metacharacter representing the number of occurrences

 *: Appear zero to multiple times

  + : Appears 1 or more times

 ? :Occurs zero or 1 times

 {n} :Occurs n times

 {n,} :Occurs n to multiple times

 {n,m}: appears n to m times

 

 reg = /^\d+wo\d+$/;  reg = /^(\d+)wo(\d+)$/;
var reg = /^0.2$/   // 以0开头 2结尾,中间可以是除了\n的任意字符
var reg = /^\d+$/;//只能是多个数字console.log(reg.test('2017'))//true//一个简单的验证手机号的正则:11位数字,第一位为1var reg = /^1\d{10}$/;
2. Application of metacharacters
 []

  1. All characters appearing in square brackets The characters are all characters that represent their own meaning (no special meaning).

 2. Square brackets do not recognize two digits

 

var reg = /^[12-68]$/ -> ;Represents one of 1, 2-6, and one of 8. This method is wrong

 ()

1. Function 1 of grouping: Change the default priority of x|y

       var reg = /^18|19$/;//符合条件的有18、19、181、189、119、819、1819
       var reg = /^(18|19)$/  //18、19
 1. Regular rules for valid numbers: positive numbers, negative numbers, zero, decimals
## 1), "." may or may not appear, but once it appears, it must be followed by Followed by one or more digits

  2), there may be +/- at the beginning or not

  3), the integer part, A single digit can be one between 0-9, and multiple digits cannot start with 0

 The regular rules are as follows

 

var reg = /^[+-]?(\d|([1-9]\d+))(\.\d+)?$/

The above is the detailed content of Introduction and application of regular metacharacters. 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