In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "JS regular expression laziness and greed example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "JS regular expression laziness and greed example analysis" this article.
Exec-> regular capture
Every capture is matched by default first. If the match is not successful, the result is that null; can only be captured if there is a match.
Captured content format
1. The captured content is an array, and the first item in the array is the current regular captured content.
Index: the index position where the capture begins in the string
Input: the original string captured
Var reg = 'woshi2016ni2017';var res = reg.exec (str); console.log (res) / / [the content captured by exec for the second time is still the first "2016" var res = reg.exec (str); console.log (res) / the content captured by exec for the second time is still the first "2016ni2017'; console.log (res) / / [2016ni2017']
2. Characteristics of regular capture
1), lazy-> each execution of exec only captures the first matching content, and without any processing, after performing multiple captures, the first matching content is still captured.
LastIndex: is the regular location where the search begins in the string each time it is captured. The default is 0.
2) how to solve laziness? Add a modifier "g" at the end of the rule
Modifiers: G, I, m
Global (g): global match
IgnoreCase (I): ignore case matching
Multiline (m): multiline matching
Var reg = /\ d Greg str = 'woshi2016ni2017';console.log (reg.lastIndex) console.log (reg.exec (str))
Principle: add the global modifier g, regular after each capture, our lastIndex value becomes the latest value, the next capture starts from the latest location, so that we can get all the content that needs to be captured.
3) write your own program to get all the content captured by the rule (don't forget to add g)
Var reg = /\ dcharger g; var str = 'aswofde2015xsewde2016awdefer2017'; var ary = []; var res = reg.exec (str); while (res) {ary.push (res [0]) res = reg.exec (str);} console.log (ary) / / [2015, 2016, 2016, 2017]
4) each capture of greedy regularity is captured according to the longest matching result, for example: 2 conforms to the regularity, 2015 also conforms to the regularity, and we capture 2015 by default
5) how to solve the regular greed-> add one after the quantifier metacharacter? It can be done.
Var reg = /\ dcake str g; var str = 'aswofde2015xsewde2016awdefer2017'; console.log (reg.exec (str))
? There are many functions in the rule:
Putting it after an ordinary metacharacter represents the occurrence of 0-1 times /\ dcharacters /-> numbers may or may not appear.
Putting it after the metacharacter of a quantifier is greedy when uncapturing.
3. The match method in the string-> gets all the characters that match the rule to the
Var reg = /\ djewelry raceme g; var str = 'aswofde2015xsewde2016awdefer2017'; var ary = str.match (reg); / / [2LECHER 0LECHERONE 5MHEROLING 0LECHING 6LINGY 2JINGOMY 0MIZOMY 7]
Although match is more convenient than our exec in the current situation, there are some problems in match that can not be dealt with by itself. In the case of packet capture, match can only capture the content that matches the big regular, but cannot get the content captured by the small regular.
These are all the contents of the article "sample Analysis of laziness and greed of regular expressions in JS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.