In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
JS regular expression details and write at the same time what meaning, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
For beginners of the rule, the two seemingly simple characters ^ $always make the matching results exceed our expectations in use. When does ^ indicate the beginning of the line and when does it mean the opposite? What does it mean to write two together? Today we will talk about these two characters in detail.
What do you mean about ^ and $?
First explain the concept of ^ and $, very simple two sentences, first make an impression.
Delimiting: matches the beginning, if there are multiple lines matching the header of multiple lines.
Dollar sign: matches the tail, if there is a multi-line matching multi-line tail.
We know that a regular is a matching pattern, either matching characters or matching positions. Here we have to explain the two characters respectively from these two matches.
What does it mean from the perspective of matching characters?
When used to match characters, ^ and $are more often used as auxiliary conditions for matching accurate results. Let's look at a simple example:
'123'.match (/\ dhampg); / / [1, 2, 2, 3]
It is easy to understand that the global (note that there is a g at the end) matches a single arbitrary number, and it is obvious that 1Magne2 and 3 meet the criteria, so here are three matching results; let's add ^ and $respectively to see:
'123'.match (/ ^\ dUnig); / / [1]
'123'.match (/\ dhammer / g); / / [3]
At this time, ^\ d can only match to 1, and\ d$ can only match to 3. You must wonder, no, isn't there a g behind my regular for a global match? why is there only one match? In the case of ^\ d, the matching condition is actually an arbitrary number after finding the beginning position, and\ d$ represents an arbitrary number before the end of the match. There is only one beginning and result for a string that does not wrap, so naturally only one number can be found. Can you now understand the meaning of ^ $as an auxiliary condition?
So now let's change the numbers and look at:
'12\ n34'.match (/ ^\ d/mg); / / [1prime3]
'12\ n34'.match (/\ d$/mg); / / [2jue 4]
Due to the existence of line breaks, there are now two beginning positions and two end positions (note that m is used in the matching to indicate a newline match), so the matching result becomes two, which is not difficult to understand.
two。 From the point of view of matching position
In addition to matching the corresponding characters according to the rules, the regular has a powerful function of matching positions. What is the location? Take character 1234 as an example, each arrow represents a position, in which the position of the first arrow is the delineated character ^, and the resulting position is the dollar character $, as shown below:
So when we are in the matching position, ^ $also becomes the result that we need to match. For example, I need to add flowers to the position at the beginning and end of 1234:
'1234'.replace (/ ^ | $/ gjingjinghuang'); / / "? 1234?"
Of course, ^ and $are also auxiliary conditions to help us pinpoint the location when matching the position, such as the commonly used thousand-bit delimiter regularity:
'12345678'.replace (/ (?! ^)) (? = (\ d {3}) + $) / ggramlin'); / / "12345678"
After reading the above, have you mastered the method of detailing JS regular expressions and writing what they mean at the same time? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.