Let’s first understand what is greedy in regular expressions and what is non-greedy? Or what is matching priority quantifier and what is ignoring priority quantifier?
Okay, I don’t know what the concept is, let’s give an example.
A student wanted to filter the content between them. This is how he wrote the regular rules and procedures.
$str = preg_replace('%<script>.+?</script>%i','',$str);//非贪婪
It seems like there is nothing wrong with it, but in fact it is not. If
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>';
then after the above program processing, the result is
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>'; $str = preg_replace('%<script>.+?</script>%i','',$str);//非贪婪 print_r($str); //$str 输出为 <script>alert(document.cookie)</script>
Still He couldn't achieve the effect he wanted. The above is non-greed, and some are called laziness. The sign of non-greedy is to add ? after the quantitative metacharacter, such as +?, *?, ?? (more special, I will write about it in the future BLOG), etc. That is, it means non-greedy. If you don’t write ?, it means greedy. For example
$str = '<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>'; $str = preg_replace('%<script>.+</script>%i','',$str);//非贪婪 print_r($str); //$str 输出为Latest Articles by Author
1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00 1970-01-01 08:00:00Latest IssuesHow to display the mobile version of Google Chrome Hello teacher, how can I change Google Chrome into a mobile version?From 2024-04-23 00:22:190112281There is no output in the parent window document.onclick = function(){ window.opener.document.write('I am the output of the child ...From 2024-04-18 23:52:34011796Related TopicsMore>
Windows 11 my computer transfer to the desktop tutorial
How to open the download permission of Douyin
Rename the apk software
The difference between while loop and do while loop
timestampdiff function usage
The difference between article tag and section tag
How to use math.round function
What are the marquee parameters?