Regular expression modifier

Pattern modifiers commonly used in regular expressions

They can be used in combination.

i does not distinguish between upper and lower case;
for example: /abc/i can match abc, aBC, Abc
g global matching
If there is no g, the string is matched from left to right during the regularization process, and the first one that meets the condition is found
If the match is successful, return. If you bring g, then the string will go from left to right, and each one that meets the conditions will be recorded and the end position of the string will be known.
For example:

var str = 'aaaaaaaa'
var reg1 = /a/;  str.match(reg1)  // 结果为:["a", index: 0, input: "aaaaaaaa"]
var reg2 = /a/g; str.match(reg2)  // 结果为:["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 to realize multiple lines, after each newline is the beginning

var str = "abcggab\nabcoab";
var preg1 = /^abc/gm;  str.match(preg1)  // 结果为:["abc", "abc"]
var preg2 = /ab$/gm;   str.match(preg2)  // 结果为:["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 matches;

$mode="/a(.*?)c/";
$preg="/a.*c/U";//这两个正则返回相同的值
$str="abcabbbcabbbbbc" ;
preg_match($mode,$str,$content);   echo $content[0];//abc
preg_match($preg,$str,$content);   echo $content[0];//abc

//Modifier: x ignores whitespace 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;
//Modify Symbol: e is used in conjunction with the function preg_replace() to execute the matched string as a regular expression;
give yourself a thumbs up every day!
The original blogger doesn’t seem to be able to see it for the time being. I created a reprint, and the article cannot be published. I am changing this blog to reprint.
The above is transferred from https://www.cnblogs.com/kevin-yuan/archive/2012/09/25/2702167.html

Guess you like

Origin blog.csdn.net/m0_51465487/article/details/115256514