In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the use of question marks in JavaScript regular expressions, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Put a question mark after the character that indicates repetition, such as +?, *?, {2pr 3}? A greedy pattern that can stop matching.
Var pattern=/\ w {2pattern2 3} /; console.log ("aaaa" .match (match) [0]); / * result "aaa"; in greedy mode, there are as many matches as possible, so three repetitive characters * / var pattern2=/\ w {2Yue3}? /; console.log ("aaaa" .match (match) [0]); / * result "aa" After adding the question mark, it will match as few repetitions as possible, * so it matches to 2 repeated characters * /
Using?: within a group can produce unnumbered groups, such as
Var pattern=/ (ab)\ w + (ba) /; console.log ("abcba_" .replace (pattern, "$1")); / * result "ab_"; matching characters are replaced by the first grouping (ab) * / var pattern2=/ (?: ab)\ w + (ba) /; console.log ("abcba_" .replace (pattern2, "$1")); / * result "ba_" A?: is added to the first grouping, resulting in a * unnumbered grouping, so the character matching $1 is the second grouping, * that is, the text content matched by the first numbered grouping (ba) * /
(? =) and (?!); zero width positive assertions and negative assertions, with parentheses indicating that the right side of a position must match the right side of the =, or disagree! The characters after the match.
Var pattern=/str (? = ings) ing/;console.log ("strings.a" .match (pattern)); console.log ("strings.a" .match (? = s) /); console.log ("string_x" .match (pattern)); console.log ("string_x" .match (/ string (= s) /)); / * the first two results are ["string"], and the last two results are null * str (? = ings) ing/ matches "string", the right position after r must be the same as * ings; and / string (? = s) /; the match "string"; the position after g must be followed by an s to the right. " "string_x" also contains "string" but * is not satisfied (? =...) Conditions in parentheses * / var pattern=/string (?! s) /; console.log ("strings" .match (pattern)); / / nullconsole.log ("string." .match (pattern)); / / ["string"] / * (?!...) It can't be on the right side of a certain position! After matching characters, * string (?! s) / match "string", "g" cannot be followed by "s" * / above are the use of question marks in JavaScript regular expressions. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.