Home > Web Front-end > JS Tutorial > body text

Usage analysis of js regular expression longest matching (greedy matching) and shortest matching (lazy matching)

高洛峰
Release: 2017-01-09 15:57:47
Original
1645 people have browsed it

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 = /^\.\//;
Copy after login

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
Copy after login

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";
Copy after login

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!


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!