In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of regular expression character class, which is very detailed and has certain reference value. Friends who are interested must finish it!
one。 Basic concepts:
The character class is relatively simple, but it may also cause some misunderstandings due to lack of in-depth understanding or carelessness. Let's introduce it again.
The character class [] can match any of a series of characters contained in square brackets, but the result of the match can only be one of them rather than multiple, for example:
Var str= "abcd"; var reg=/ [abcd] /; console.log (str.match (reg))
The above regular expression can only match the character "a".
two。 Use the hyphen "-":
Character classes can use hyphens "-" to determine a matching range. Of course, there are principles for using hyphens. There is an order between the two characters. If the same encoding is used, the following character code point should be greater than or equal to the code point of the previous character, for example:
[0-9] / / correct
[9-0] / / wrong
three。 Whether special characters in character classes need to be escaped:
Most characters with special meaning in the rule need to be escaped when matching themselves, such as "." You can match any character, "$" is used to match the end of the string, and there is no need to escape in the character class. The code example is as follows:
Var str= "abc.efcd"; var reg=/c\. / gharma console.log (str.match (reg))
In the above rule, a backslash is added before the period for escape, so only the string "c." will be matched, otherwise it will also match "cd".
However, most metacharacters in character classes do not need to be escaped. The only characters that must be escaped are "\", "^" and "-" depending on the specific application scenario. The code example is as follows:
Example 1:
Var str= "ab$c.efcd"; var reg=/ [$.] / gscape console.log (str.match (reg))
The above code can match the "$" and "." in the string. Itself.
Example 2:
Var str= "a\\ b$c.efcd"; var reg=/ [\\ $.] / gholders console.log (str.match (reg))
The above code must be escaped to match the backslash.
Example 3:
Var str= "ab- c ^ 569"; var reg=/ [\ ^ 0\-9] / gorientation console.log (str.match (reg))
In the above code, "^" and "-" no longer have special meaning, but represent themselves because of the use of escaping.
Example 4:
Var str= "ab- c ^ 569"; var reg=/ [- a ^ c0] / gscape console.log (str.match (reg))
The above codes "-" and "^" are not escaped because their location makes them lose their special meaning.
Escape characters can also be used in character classes:
The code example is as follows:
Var str= "abc
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.