Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the daily collection of JS regular expressions?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the daily collection of JS regular expressions, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

RegExp direct quantity and object creation

Just like strings and numbers, it is obvious that every primitive type direct quantity with the same value in the program represents the same value. When the program runs, it creates a new object every time it encounters direct object quantities (initialization expressions) such as {} and []. For example, if you write var a = [] in the body of the loop, a new empty array is created for each iteration. In contrast, the ECMAScript 3 specification states that a regular expression direct quantity is converted to a RegExp object when executed to it, and each operation of the regular expression direct quantity represented by the same code returns the same object. The ECMAScript 5 specification does the opposite, and each operation on the direct amount of a regular expression represented by the same code returns a new object. I E has always been implemented in accordance with the EC-MAScript 5 specification, and most of the latest versions of browsers have begun to follow EC-MAScript 5, although the standard has not been widely implemented.

1.1 Direct character measurement

All letters and numbers in a regular expression are matched literally. JavaScript regular expression syntax also supports non-alphabetic character matching, which needs to be escaped with a backslash (\) as a prefix. For example, escape characters\ nare used to match newline characters. These escape characters are listed in Table 10-1.

Table 10-1: direct quantitative characters in regular expressions

Characters match alphanumeric characters themselves\ oNUL characters (\ u0000)\ t tabs (\ u0009)\ nnewline characters (\ u000A)\ v vertical tabs (\ u000B)\ f feed characters (\ u000C)\ r carriage returns (\ u000D)\ xnn Latin characters specified by the hexadecimal number nn, for example,\ X0A is equivalent to\ n\ uxxxx Unicode characters specified by the hexadecimal number xxxx, for example,\ u0009 is equivalent to\ t\ cX control character ^ X For example,\ cJ is equivalent to a newline character\ nIn regular expressions, many punctuation marks have a special meaning: ^ $. * +? =!: |\ / () [] {}

The regular expression "/ /" is used to match any string that contains a backslash.

1.2 character class

For example, / [\ u0400 -\ u04FF] / is used to match all Cyrillic characters.

Table 10-2: character classes for regular expressions

Character matching [.] Any character in square brackets [^.] Any character that is not in square brackets. Any character except newline character and other Unicode line Terminator\ w A word consisting of any ASCII character is equivalent to [a-zA-Z0-9]\ W any word that is not an ASCII character, equivalent to [^ a-zA-Z0-9]\ s any Unicode blank character\ S any character that is not a Unicode white space character, note that\ w and\ S are different\ d any ASCII number is equivalent to [0-9]\ D any character except the ASCII number Equivalent to [^ 0-9]\ b backspace direct quantity (special case)

Note that these special escape characters can also be written within square brackets. For example, because\ s matches all white space characters and\ d matches all numbers, / [\ s\ d] / matches any white space character or number. Note that there is a special case. Below we will see the special meaning of the escape character\ b. When used in a character class, it represents a backspace character, so to represent a backspace character in a regular expression in a direct amount, you only need to use the character class / [\ b] / with one element.

1.3 repeat

We follow the regular pattern with the tag used to specify the repetition of the character. Because some repetition types are not often used, there are some special characters specifically used to indicate this situation. For example, "+" is used to match one or more copies of the previous pattern. Table 10-3 summarizes these regular syntax for repetition.

Table 10-3: repetitive character syntax for regular expressions

The meaning of the character {n ·m} matches the previous term at least n times, but not more than m times {n,} at least n times {n} matches the previous top n times? Match the previous term 0 times or 1 time, that is to say, the previous term is optional, which is equivalent to {0score1} + matching the previous term 1 or more times, equivalent to {1,} * matching the previous term 0 or more times, equivalent to {0,}

Here are some examples:

/\ d {2pr 4} / / match 2'4 digits

/\ w {3}\ dwords / exactly match three words and an optional number

/\ s+java\ spaces / matches the string "java" with one or more spaces before and after it

/ [^ (] / matches one or more characters that are not left parentheses

Using "and"? Note that because these characters may match 0 characters, they allow nothing to match. For example, the regular expression / bbbb / actually matches the string "regular" because it contains 0 a.

Non-greedy repetition

The matching repetition characters listed in Table 10-3 match as many as possible and allow subsequent regular expressions to continue to match. Therefore, we call it "greedy" matching. We can also use regular expressions for non-greedy matching. Just follow a question mark after the character to be matched: "?", "+", "*?" Or "{1pm 5}?". For example, the regular expression / asides / can match one or more consecutive letters a. When "aaa" is used as the matching string, the regular expression matches its three characters. But / a consecutive letter / can also match one or more consecutive letters a, but it matches as few as possible. We also use "aaa" as the matching string, but the latter pattern can only match the first a.

The results obtained by using non-greedy matching patterns may not be consistent with expectations. Consider the following regular expression / aaccounbhand, which can match one or more a, as well as a b. When "aaab" is used as the matching string, it matches the entire string. Now try the non-greedy matching version of / a greedy match, which matches as few an and a b as possible. When you use it to match "aaab", you expect it to match an and the last b. But in fact, the pattern matches the entire string, exactly the same as the greedy match of the pattern. This is because the pattern matching of regular expressions always looks for the first possible match in the string. Because the match starts with the first character of the string, the shorter match in its substring is not considered here.

1.4 selection, grouping and referencing

The syntax of a regular expression also includes specifying selections, grouping subexpressions, and special characters that reference the previous subexpression. The character "|" is used to separate characters for selection. For example, / ab | cd | ef/ can match the string "ab", the string "cd", or the string "ef". /\ d {3} | [amurz] {4} / matches three digits or four lowercase letters.

Notice that the order in which the selections attempt to match is from left to right until a match is found. If the selection on the left matches, the match on the right is ignored, even if it produces a better match. Therefore, when the regular expression / a | ab/ matches the string "ab", it can only match the first character.

Table 10-4: selection, grouping, and reference characters of regular expressions

Character meaning | Select, matching the subexpression on the left or the subexpression on the right of the symbol. Combination, the combination of several items into a unit, this unit can be through "*", "+", "?" Decorate it with symbols such as "|", and you can remember the string that matches this combination for subsequent references (?:.) Only combine, combine items into a unit, but do not remember the characters that match the group\ n and the characters matched for the nth group for the first time. The group is a subexpression in parentheses (or it may be nested). The group index is the number of left parentheses from left to right, and the grouping in the form of "(?:" is not encoded.

1.5 specify matching location

As mentioned earlier, only multiple elements in a regular expression can match one character of a string. For example,\ s matches only a blank character. There are also some regular expression elements that match the position between characters rather than the actual characters.

The most common anchor element is ^, which is used to match the beginning of the string, and the anchor element $is used to match the end of the string.

Table 10-5: anchor characters in regular expressions

The meaning of the character ^ matches the beginning of a string, in multiline retrieval, the beginning of a line $matches the end of a string, and in multiline retrieval, the end of a line matches the boundary of a word, in short, it is located between the characters\ w and\ W, or between the character\ w and the beginning or end of the string (but note [\ b] matches the backspace character)\ B matches the position of the non-word boundary (? = p) zero width positive antecedent assertion, which requires that the next characters match p, but does not include those characters that match p (?! P) zero width negative antecedent assertion, requires that the next characters do not match p

1.6 modifier

There is one last piece of knowledge about the syntax in regular expressions, that is, the modifiers of regular expressions, which are used to describe the rules for advanced matching patterns. Unlike the regular expression syntax discussed earlier, modifiers are placed outside the "/" symbol, that is, they do not appear between the two slashes, but after the second slash. JavaScript supports three modifiers, and the modifier "I" indicates that pattern matching is case-insensitive. The modifier "g" indicates that pattern matching should be global, that is, all matches in the retrieved string should be found. The modifier "m" is used to perform matches in multiline mode, where if the string to be retrieved contains multiple lines, the ^ and $anchor characters match the beginning and end of each line in addition to the beginning and end of the entire string. For example, the regular expression / java$/im can match "java" or "Java\ nis fun".

Table 10-6: regular expression modifiers

The character meaning I performs a case-insensitive match g to perform a global match, in short, to find all matches, instead of stopping the m multi-line matching pattern after finding the first one, ^ matches the beginning of a line and the beginning of a string, $matches the end of a line and the end of a string

two。 String method for pattern matching

2.1search ()

Its argument is a regular expression that returns the starting position of the first matching substring, and returns-1 if no matching substring is found.

If the argument to search () is not a regular expression, it is first converted to a regular expression through the RegExp constructor, and the search () method does not support global retrieval because it ignores the modifier g in the regular expression parameter.

"JavaScript" .search (/ script/i); / / 4

2.2replace ()

The replace () method is used to perform retrieval and replace operations. The first parameter is a regular expression and the second parameter is the string to be replaced.

"JavaScript" .replace (/ javascript/gi, "a") / / "a" / / A paragraph of quoted text starts with quotation marks, and the content area that ends in the middle of / / cannot contain quotation marks var quote = / "([^"] *) / g _ bang / replace English quotation marks with Chinese half-corner quotes, while keeping the contents between quotation marks (stored in $1) unmodified text.replace (quote,'$1')

2.3match ()

The match () method is the most commonly used String regular expression method. Its only argument is a regular expression (or converted to a regular expression through the RegExp () constructor), which returns an array of matching results.

"1 plus 2 equals 3" .match / / returns ["1", "2", "3"] for example, parse a URL:var url = / (\ w +):\ / ([\ w.] +)\ / (\ S*) /; var text = "Visit my blog at http://www.example.com/~david";var result = text.match (url)" If (result! = null) {var fullurl = result [0]; / contains "http://www.example.com/~david" var protocol = result [1]; / contains" http "var host = result [2]; / / contains" www.example.com "var path = result [3]; / contains" ~ david "}

2.4split ()

This method is used to split the string calling it into an array of substrings, using a delimiter that is an argument to split ()

"123456789" .split (","); / / return ["123,456,789"]

The argument to the split () method can also be a regular expression, which makes the split () method extremely powerful. For example, you can specify a delimiter that allows as many spaces as you want on both sides:

"1, 2, 3, 4, 5" .split (/\ slots,\ slots /); / / return ["1", "2", "3", "4", "5"]

3.RegExp object

Regular expressions are represented by RegExp objects. In addition to the RegExp () constructor, the RegExp object supports three methods and some properties.

The RegExp () constructor takes two string parameters, the second of which is optional, and RegExp () is used to create a new RegExp object. The first parameter contains the body of the regular expression, that is, the text between the two slashes in the regular expression. It is important to note that both string literals and regular expressions use the "\" character as the prefix for the escape character, so when you pass in a regular expression represented by a string to RegExp (), you must replace "\" with "\". The second argument to RegExp () is optional, and if it is supplied, it specifies the modifier of the regular expression. However, only the modifiers g, I, m, or a combination of them can be passed in. For example:

/ / globally match the five digits in the string. Note that "\" is used here instead of "\" var zipcode = new RegExp ("\ d {5}", "g").

3.1Properties of RegExp

Each RegExp object contains five properties. The property source is a read-only string that contains the text of the regular expression. The property global is a read-only Boolean value indicating whether the regular expression has the modifier g. The property ignore-Case is also a read-only Boolean value that indicates whether the regular expression has the modifier I. The property multiline is a read-only Boolean value that indicates whether the regular expression has a modifier m. The last property, lastIndex, is a read / write integer. If the matching pattern has a g modifier, this property is stored in the entire string at the beginning of the next retrieval, which is used by the exec () and test () methods, as described below.

Property describes whether the FFIEglobalRegExp object has a flag g. Whether the 14ignoreCaseRegExp object has a flag I. 14lastIndex an integer indicating the position of the character at which the next match begins. Whether the 14multilineRegExp object has the flag m. The source text of the 14source regular expression. fourteen

3.2Methods of RegExp

The RegExp object defines two methods for performing pattern matching operations. Their behavior is very similar to the String method described above. The main way for RegExp to perform pattern matching is exec (), which is similar to the String method match () introduced in Section 10.2, except that the parameter of the RegExp method is a string, while the parameter of the String method is a RegExp object.

Method describes how FFIEcompile compiles regular expressions. 14exec retrieves the value specified in the string. Returns the value found and determines its location. 14test retrieves the value specified in the string. Returns true or false. fourteen

3.2.1exec ()

Var pattern = / Java/g;var text = "JavaScript is more fun than Java!"; var result;while ((result = pattern.exec (text))! = null) {alert ("Matched'" + result [0] + "'" + "at position" + result.index + "; next search begins at" + pattern.lastIndex);}

3.2.2test ()

Another RegExp method is test (), which is simpler than exec (). Its argument is a string that detects a string with test (), and returns true if it contains a matching result of the regular expression:

Var pattern = / java/i;pattern.test ("JavaScript"); / / returns true

4. Common regular expressions

/ / phone number

/ ^ ([\ +] [0-9] {1jue 3} ([\.\ -])?)? ([\ (] [0-9] {1J 6} [\)])? ([0-9\.] {1J 32}) (([A-Za-z\:] {1jue 11})? [0-9] {1J 4}?) $/

/ / mailbox

/ ^ (([amurz] |\ d | [! #\ $% &'\ *\ +\ -\ / =\?\ ^ `{\ |} ~] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) + (\. ([amurz] |\ d | [! #\ $% &'\ *\ +\ -\ / =\?\ ^` {\ |} ~] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF] )) +) *) | (\ x22) (\ x20 |\ x09) * (\ x0d\ x0a))? (\ x20 |\ x09) +)? ([\ x01 -\ x08\ x0b\ X0c\ x0e -\ x1f\ x7f] |\ x21 | [\ x23 -\ x5b] | [\ x5d -\ x7e] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (\\ ([\ x01-\ x09\ x0b\ x0c\ x0d-\ x7f]) | U00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) * (\ x20 |\ x09) * (\ x0d\ x0a))? (\ x20 |\ x09) +)? (\ x22)) @ (([a-z] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | ([amurz] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) ([aafaz] |\ d | | |\. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) * ([aanthz] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]))\) + (([amurz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | ([aMuz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) D |-|\. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) * ([Amurz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF])\. $/ I

/ / date (YYYY-MM-DD)

/ ^\ d {4} [\ /\] (0? [1-9] | 1 [012]) [\ / -] (0? [1-9] | [12] [0-9] | 3 [01]) $/

/ / IPV4

/ ^ ([01]? [0-9] {1pr 2}) | (2 [0-4] [0-9]) | (25 [0-5])) [.]) {3} (([0-1]? [0-9] {1J 2}) | (2 [0-4] [0-9]) | (25 [0-5])) $/

/ / URL

/ ^ (https? | ftp):\ /\ / ([amurz] |\ d |-|\. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [!\ $&'\ (\)\ *\ + =] |:) * @)? ((\ d | [1-9]\ d | 1\ d\ d | 2 [0-4]\ d | 25 [0-5])\. (\ d | [1-9]\ d | 1\ d\ d | 2 [0-4]\ d | 25 [0-5]). (\ d | [1-9]\ d | 1\ d\ d | 25 [0-5])\. (\ d | [1-9]\ d | 1\ d\ d | 2 [0-4]\ d | 25 [0-5])\. (\ d | [1-9]\ d | 2 [0-4]\ d | 0-5]) | (([amurz] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | ([akoz] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) ([alyz] |\ d | -. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) * ([amerz] |\ d | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF]) (\ uFDF0-\ uFFEF])\) + (([Amurz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | ([Amurz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) ([alyz] |\ d |-|\. | _ | | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) * ([Alyz] | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF]) UFDF0-\ uFFEF])\.) (:\ d*)) (\ / (([aripz] |\ d | -. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [!\ $&'\ (\)\ *\ + =] |: @) + (\ / ([amurz] |\ d |-|\. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [!\ $&'\ (\)\ *\ + =] |: | @) *)?)? ([amurz] |\ d | -. | _ | ~ | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [!\ $&'\ (\)\ *\ + =] |: | @) | [\ uE000-\ uF8FF] |\ / |\) *)? (\ # (([aMaiz] |\ d |-|\. | _ | | [\ u00A0 -\ uD7FF\ uF900-\ uFDCF\ uFDF0-\ uFFEF]) | (% [\ da-f] {2}) | [!\ $&'\ (\)\ *\ +,; =] |: @) |\ / |\) *)? $/ I

/ / number (+-123123.123 allowed)

/ ^ [\ -\ +]? (([0-9] {1jue 3}) ([,] [0-9] {3}) *) | ([0-9] +))? ([\.] ([0-9] +))? $/

/ / 2-20 English or Chinese characters

/ ^ ([\ u4e00 -\ u9fa5] {2pm 20}) $| ^ ([a-zA-Z] {2pm 20}) $

The above is all the content of the article "what are the regular expressions of JS on a daily basis?" 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report