The example of this article analyzes the usage of longest matching (greedy matching) and shortest matching (lazy matching) of js regular expressions. Share it with everyone for your reference, the details are as follows:
I was reading the RequireJS 2.1.15 source code recently. A series of variables are defined at the beginning of the source code, and there are 4 regular expressions:
var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g, jsSuffixRegExp = /\.js$/, currDirRegExp = /^\.\//;
commentRegExp Regular expressions are used to match comments in JavaScript code. For the usage of /m, please refer to this article: Usage analysis of multiline (/m) in JS regular expression modifiers. For the usage of /g, please refer to this article: JS regular expression modifiers. global(/g) usage analysis. I have never seen this usage of *? in commentRegExp before, and I think it is very strange, because * represents 0 or any more in regular expressions, and ? represents 0 or 1. At first, I thought this way of writing *? was redundant. . I asked my colleagues to find out that *? This way of writing is lazy matching.
alert(/abc([\w]*)/mg.exec("abc1abc2")[0]);//abc1abc2 alert(/abc([\w]*?)/mg.exec("abc1abc2")[0]);//abc
It can be seen through the following code: The difference between the longest match and the shortest match can be seen through this code: The difference between the longest match and the shortest match, one matches as many characters as possible, and the other Match as few characters as possible. Generally, regular expression engines use the longest match by default. If we want the shortest match, we can add a ? after the quantity modifier to become the shortest match.
/***注释1****/ var name = "aty"; /***注释2****/ var name = "aty";
Through the above code, we can know why requirejs uses the shortest matching mode of *? when matching javascript comments. If we want to delete all comments, the shortest match should be used, otherwise the var name="aty"; code will be replaced.
For more js regular expression longest matching (greedy matching) and shortest matching (lazy matching) usage analysis related articles, please pay attention to the PHP Chinese website!