In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand JavaScript regular expressions, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
1. Regular expression creation
JavaScript has two ways to create regular expressions:
The first one: directly through / regular expression / write
Second: create a RegExp object through new RegExp ('regular expression')
Const re1 = / ABC\-001bot Const re2 = new RegExp ('ABC\\-001'); re1; / ABC\-001ax re2; / / ABC\-001 /
Note that if you use the second way of writing, the two\ s of the string are actually one\ because of the escape problem of the string.
two。 Use pattern 2.1 use simple mode
A simple pattern is made up of direct matches found. For example, the pattern / abc/ matches in a string where only the characters' abc' appear at the same time and in that order. In "Hi, do you know your abc's?" And "The latest airplane designs evolved from slabcraft." It will be a match. In the above two examples, the matching is the substring 'abc'. It will not be matched in the string "Grab crab" because it does not contain any 'abc' substrings.
2.2 use special characters
For example, the pattern / abc/ matches a single'a 'followed by zero or more' b'(meaning zero or more in the previous term), followed by any combination of characters'c'. In the string "s'scbbabbbbcdebc", this pattern matches the substring "abbbbc".
Character meaning\ matching will follow the following rules:
A backslash before a non-special character indicates that the next character is special and cannot be literally interpreted. For example, a'd 'without a''in front usually matches a lowercase'd'. If you add'', the character becomes a character with a special meaning, meaning to match a number.
The backslash can also escape the following special characters to a literal amount. For example, the pattern / a / representation matches 0 or more a. Instead, the pattern / asides / removes the particularity of''so that it can match strings like "a*".
Don't forget to escape\ when using new RegExp ("pattern"), because\ is also an escaped character in a string. ^ matches the beginning of the input, for example, / ^ A / does not match'A'in'an A', but does match'A'in'An E'. The end of the matching input. For example, / tweak / does not match the 'tweak' in 'eater', but it does match the't'in 'eat'. * match the previous expression 0 or more times. Equivalent to {0,}. For example, / bo*/ matches' booooo'+ in "A ghost boooooed" to match the previous expression one or more times. Equivalent to {1,}. For example, / asides / matches the 'aways' in "candy" and all the 'ais "in" caaaaaaandy ". ? Match the previous expression 0 or 1 times. It is equivalent to {0jue 1}. For example, / e?le?/ matches' el','in "angel" and 'le' in "" angle "and' l' in" oslo'.
If it is closely followed by any quantifier *, +,? Or {} will make the quantifier non-greedy (matching as few characters as possible), as opposed to the default greedy pattern (matching as many characters as possible).
For example, applying /\ dholder / to "123abc" will return "123", but if you use /\ dholder matching bank, it will only match to" 1 ". . Matches any single character except the newline character. For example, / .n / will match 'an' and' on', but not 'nay'' in "nay, an apple is on the tree". Xy {n} n is a positive integer that matches the previous character exactly n times.
For example, / a {2} / will not match'a'in 'candy', but will match all'a'in 'caandy' and the first two'a'in 'caaandy'. {ndirection m} n and m are integers. Match the previous characters at least n times and m times at most. If the value of n or m is 0, this value is ignored. For example, / a {1,3} / does not match any character in "cndy", matches an in "candy", matches the first two an in "caandy", and matches the first three an in "caaaaaaandy". Note that when matching "caaaaaaandy", the matching value is "aaa", even if there are more an in the original string. [xyz] A collection of characters. Match any characters in parentheses, including escape sequences. You can use a dash (-) to specify a range of characters. Special symbols such as dots (.) and asterisks (*) have no special meaning in a character set. They don't have to escape, but they also work.
For example, [abcd] and [amurd] are the same. They all match the 'baked' in "brisket" and the'c'in "city". / [a murz.] + / and / [\ w.] + / match the string "test.i.ng". [^ xyz] A reverse character set. That is, it matches any characters that are not contained in square brackets. You can use a dash (-) to specify a range of characters. Any ordinary character works here. \ b matches the boundary of a word. The boundary of a word is the position where a word is not followed by another "word" character or where there is no other "word" character in front of it. Note that the boundary of a matching word is not included in the matching content. In other words, the length of the content of the boundary of a matching word is 0. For example:
/\ bm/ matches the 'oo',' in 'moon' and does not match the 'oo',' in 'moon' because the 'character' is followed by the character'n'. / oon\ b / matches the 'oon', because' oon' in "moon" is the end of the string. In this way he was not followed by a "word" character. \ d matches a number. Equivalent to [0-9]. For example, /\ d / or / [0-9] / matches "B2 is the suite number." The'2' in. \ D matches a non-numeric character. Equivalent to [^ 0-9]. For example, /\ D/ or / [^ 0-9] / matches "B2 is the suite number." The'B'in. \ f matches a feed character (Utt000C). \ nmatches a newline character (Ubun000A). \ r matches a carriage return (Udest000D). \ s matches a white space character, including spaces, tabs, page breaks, and line breaks. Equivalent to [\ f\ n\ r\ t\ v\ u00a0\ u1680\ u180e\ u2000 -\ u200a\ u2028\ u2029\ u202f\ u205f\ u3000\ ufeff].
For example, /\ s\ wicked / matches "foo bar." The 'bar'' in. \ s matches a non-white space character. Equivalent to [^\ f\ n\ r\ t\ v\ u00a0\ u1680\ u180e\ u2000 -\ u200a\ u2028\ u2029\ u202f\ u205f\ u3000\ ufeff].
For example, /\ S\ wicked / matches "foo bar." The 'foo'' in. \ t matches a horizontal tab (Ubun0009). \ w matches a single word character (letter, number, or underscore). Equivalent to [A-Za-z0-9].
For example, /\ w / matches' asides in "apple,"$5.28,"5' and" 3D. " The'3' in. \ W matches a non-word character. \ nin a regular expression, it returns the last nth child capture matching substring (the number of captures is counted in left parentheses). 3. Apply 3.1 syncopation string
Segmenting a string with a regular expression is more flexible than using a fixed character, the usual syncopation code:
'a d c'.split (''); / / ['asides,' dudes,'c']
The above method does not recognize consecutive spaces and uses regular expressions instead:
'a b c'.split (/\ sfolk /); / / ['await,' baked,'c']
No matter how many spaces can be divided properly. Then add',':
'a d'.split b, c d'.split (/ [\ s\,] + /); / / ['averse,' baked, 'caged,' d']
Join again;:
'a d'.split bten; c BX (/ [\ s\,\;] + /); / / ['averse,' baked, 'cached,' d']
Therefore, regular expressions can be used to convert irregular input into the correct array.
3.2 grouping
In addition to determining whether there is a match, the regular expression can also extract the substring, which is represented by () the Group to be extracted. For example:
^ (\ d {4})-(\ d {4pm 9}) $defines two groups respectively, and you can extract the area code and local number directly from the matching string:
Var re = / ^ (\ d {4})-(\ d {4je 9}) $/; re.exec ('0530-12306'); / / ['010-12345', '010-12345'] re.exec ('0530 12306'); / / null
After a successful match, the exec () method returns an array, the first element being the entire string to which the regular expression is matched, and the subsequent string representing the substring that matches successfully.
The exec () method returns null if the match fails.
3.3 greedy matching
Note that regular matching defaults to greedy matching, that is, matching as many characters as possible. Match the 0 after the number as follows:
Var re = / ^ (\ d +) (0*) $/; re.exec ('102300'); / / ['102300,' 102300,']
Because\ d + uses greedy matching, all the subsequent zeros are matched directly, and the result is that 0* can only match the empty string.
You must let\ d + use non-greedy matching (that is, as few matches as possible) in order to match the following zeros, add one? You can make\ d + use non-greedy matching:
Var re = / ^ (\ dwords?) (0*) $/; re.exec ('102300'); / / ['102300', '1023', '00'] 3.4 regular expressions mark g global search. I search is not case-sensitive. M multi-line search. Y performs a "sticky" search, matching starts at the current position of the target string, and you can use the y flag. 3.5 test () method
The test () method is used to detect whether a string matches a pattern, and returns true if the string contains matching text, otherwise it returns false.
Var re = / ^ (\ d {4})-(\ d {4je 9}) $/; re.test ('0530-12321'); / / truere.test ('0530-123ab'); / / falsere.test (' 0530 12321'); / / false4. Use regular (reference) to verify Email address: ^\ w + [- +.]\ w +) * @\ w + ([-.]\ w +) *\.\ w + ([-.]\ w +) * $verify ID number (15 or 18 digits): ^\ d {15} |\ d {} 18$ Chinese mainland Mobile number: 1\ d {10} Chinese mainland fixed number: (\ d {4}-|\ d {3} -) )? (\ d {8} |\ d {7}) Chinese mainland Postal Code: [1-9]\ d {5} IP address: (2 [0-4]\ d | 25 [0-5] | [01]?\ d\ d?) {3} (2 [0-4]\ d | 25 [0-5] | [01]?\ d\ d?) Date (year-month-day): (\ d {4} |\ d {2})-(1 [0-2]) | (0? [1-9]))-(([12] [0-9])) | (3 [01]) | (0? [1-9]) date (month / day / year): (1 [0-2]) | (0? [1-9])) / (([12] [0-9])) | 01]) | (0? [1-9])) / (\ d {4} |\ d {2}) Verification digits: ^ [0-9] * $verify n digits: ^\ d {n} $verify at least n digits: ^\ d {n } $verify the number of mmurn digits: ^\ d {m N} $verify zero and non-zero beginning numbers: ^ (0 | [1-9] [0-9] *) $verify positive real numbers with 1-3 decimal places: ^ [0-9] + (. [0-9] {1pr 3})? $verify non-zero positive integers: ^\ +? [1-9] [0-9] * $verify non-zero negative integers: ^\-[1-9] [0-9] * $verify non-negative integers (positive integer + 0) ^\ daugh $verify non-positive integer (negative integer + 0) ^ ((-\ d +) | (0 +)) $verify characters of length 3: ^. {3} $verify a string of 26 letters: ^ [A-Za-z] + $verify a string of 26 uppercase letters: ^ [Afuz] + $verify a string of 26 lowercase letters: ^ [an A-Za-z0] + $verify the string consisting of numbers and 26 letters: ^ [Murz-9] + $finish reading the above Do you have any further understanding of how to understand JavaScript regular expressions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.