Regular modifier

Commonly used pattern modifiers in regular expressions are i, g, m, s, U, x, a, D, e, etc.

They can be used in combination.

i ignore case; 
for example: /abc/i can match abc, aBC, Abc
Global g (global) matching 
Without g, during the regular matching string from left to right, to find the first matching, i.e., matches, return 
if the tape g, the string from left to right, to find each qualifying Record the conditions and know the end position of the string 
For example: 
var str ='aaaaaaaa' 
var reg1 = /a/; str.match(reg1) // The result is: ["a", index: 0, input: "aaaaaaaa "] 
var reg2 = /a/g; str.match(reg2) // The result is: ["a", "a", "a", "a", "a", "a", "a", "a"]
m Multiple (more) line matching 
If there is a newline \n and there is a start ^ or end $ character, use it with g to achieve global matching, 
because when there is a newline, the newline character will be treated as a character by default. The task matching string is a single line , 
G only matches the first line. After adding m, multiple lines are realized. After each newline character is the beginning 
var str = "abcggab\nabcoab"; 
var preg1 = /^abc/gm; str.match(preg1) // The result is :["Abc", "abc"] 
var preg2 = /ab$/gm; str.match(preg2) // The result is: ["ab", "ab"]


s Special character dot. Contains a newline character. The 
default dot. Matches any single character except the newline character \n. After adding s, the. contains a newline character 
$str = "abggab\nacbs"; 
$preg = "/b./s"; 
preg_match_all($preg, $str,$matchs); 
print_r($matchs);//Array ([0] => Array ([0] => bg [1] => b [2] => bs)) 

U only matches the most recent string; no repeated matching; 
$mode="/a(.*?)c/"; 
$preg="/a.*c/U";/ /These two 
regulars return the same value $str="abcabbbcabbbbbc"; 
preg_match($mode,$str,$content); echo $content[0];//abc 
preg_match($preg,$str,$content); echo $content[0];//abc 
//Modifier: x ignores the blanks in the pattern; 
//Modifier: A forces to match from the beginning of the target string; 
//Modifier: D If you use $ to limit the ending character, No newline at the end is allowed; 
//Modifier: e is used in conjunction with the function preg_replace(), and the matched string can be executed as a regular expression;  

Guess you like

Origin blog.csdn.net/weixin_43932088/article/details/88013958