In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "JavaScript regular expression use case analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "JavaScript regular expression use case analysis" it!
Background
I have a Node service that wants to mark incoming requests. If the incoming path is a route I defined, it will mark a REQ, otherwise it will mark an IVL to output the log records of the entire service. Then I generate a RouterMap according to the defined route when the service starts, and determine whether the path hits the RouterMap or not when the access enters.
The approximate code is as follows:
Export function getSourceMak (routerMap: AppRouterMap [], req: http.IncomingMessage,): SourceMark.REQ | SourceMark.IVL | SourceMark.TST {const {url, method, headers} = req; const pathname = url.split ('?) [0]; const userAgent = headers ['user-agent']; / / Security scan if (userAgent?.includes (' TST (Tencent') & & userAgent.includes ('Team)') {return SourceMark.TST } for (const item of routerMap) {const {reg} = item; if (reg.test (pathname) & & item.method = method.toLocaleLowerCase ()) {return SourceMark.REQ;}} return SourceMark.IVL;}
Because of some dynamic routing reasons, the equality judgment can not be made directly through path. It is necessary to generate a corresponding regular expression for the corresponding routing rules, which is generated when the service is started and saved in memory for reuse.
The normal code is generated as follows:
Export function createRouterRegexp (url) {const urlBlock = url.split ('/'); const regBlock = urlBlock.map ((block) = > {if (block [0] = =':') {return'((?! /).) *';} return block;}); return new RegExp (`^ ${regBlock.join ('/')} $`, 'ig');} question
Then I found a strange phenomenon when I was debugging. Suppose I have a route of GET / cats/find, and I find the corresponding regular expression by typing. When / ^\ / cats\ / find$/gi matches / cats/find, the first is true, the second is false, the third is true, the fourth is false, and so on.
After repeated verification, there is no problem with the node code, and there is no problem with the regular expression, so I try to reproduce it in the browser and get the same problem. At this point, I'm pretty sure that there must be some regularization-related pitfalls that I haven't noticed before. So I checked the JavaScript documents, and I finally found out why.
Reason
By looking for documents related to MDN regularities, the following instructions are found
"nolink" > use test () when setting the regularity of the global flag
If the regular expression has a global flag set, the execution of test () alters the regular expression lastIndex property. The test () method is executed continuously, and subsequent execution will start to match the string at lastIndex, (exec () also changes the value of the lastIndex attribute of the rule itself).
The following example shows this behavior:
Var regex = / foo/g; / / regex.lastIndex is at 0 regex.test ('foo'); / / true / / regex.lastIndex is now at 3 regex.test (' foo'); / / false
RegExp.prototype.test ()-JavaScript | MDN
Isn't that the problem I have?
According to the documentation, when the regular expression is matched globally with the g flag, there will be a lastIndex attribute in the regex instance to record the subscript + 1 of the last bit of the rule, which is used to match from lastIndex the next time test is called. I have never encountered a high probability before because of the following reasons:
Each time a regular check is performed, the regular instance is regenerated: / ^\ / cats\ / find$/gi.test ('/ cats/find').
But because this time I will save the regular instance and use it repeatedly. Which leads to problems.
And through verification, when the match is successful, lastIndex will record the position of the next start, but when the match fails, lastIndex will return to zero and start from scratch.
At this point, the experience of being trapped took about 60 minutes, which delayed the best time to eat, resulting in a shortage of food in the canteen. But at the same time, it also gains a hole in JavaScript's regularity that is easy to be ignored. There seems to be nothing to lose.
At this point, I believe you have a deeper understanding of "JavaScript regular expression use case analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.