In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "sample analysis of js regular expressions", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of js regular expressions".
Regular expressions: search, replace, and extract operations for information in a string. (comments and whitespace are not supported and must be written on one line)
Creation of regular expressions: characters contained between a pair of slashes (direct quantity syntax)
For example:
Var pattern = / s destroy _ pattern; / / create a regular to match all strings that end with the letter s and assign a value to the string
I. character class
Character classes are formed by placing the direct amount of characters in square brackets separately.
A character class can match any character it contains.
Character classes for regular expressions:
[...] Any character in square brackets
[^.] Any character that is not in square brackets
. Any character except newline characters and other Unicode line Terminators
\ w any word consisting of ASCII characters is equivalent to [a-zA-Z0-9]
\ W any word consisting of non-ASCII characters is equivalent to [^ a-zA-Z0-9]
\ s any Unicode space character
\ s any non-Unicode white space character
\ d any ASCII number is equivalent to [0-9]
\ d any non-ASCII number is equivalent to [^ 0-9]
[\ b] backspace direct quantity (special case)
For example: / [a _ c] / means any one of the letters _ c matches.
/ [\ s\ d] / means to match any white space character or number
2. Repetition
A mark used to indicate the repetition of a specified character
Repetitive character syntax for regular expressions:
The previous term is matched at least n times, but not more than m times.
{n,} match the previous term n or more times
{n} match the previous term n times
? Match the previous item 0 times or 1 time, that is, the previous term is optional, which is equivalent to {0quotil 1}
+ match the previous item one or more times, equivalent to {1,}
* match the previous item 0 or more times, equivalent to {0,}
For example: /\ d {2pr 4} / match 2x 4 digits
/\ w {3}\ d / exactly match three words and an optional number
III. Selection, grouping and citation
Matching program: from left to right, if the match on the left matches, the match on the right is automatically ignored (even if a better match can be produced)
① | used to separate characters for selection
For example: / ab | cd | ef/ can match the strings "ab", "cd" or "ef"
/\ d {3} | [amurz] {4} / matches three digits or four lowercase letters
② ()
Function one: synthesize individual items into subexpressions
Function 2: define subpatterns in a complete pattern
When a regular expression successfully matches the target string, the part that matches the subpattern in parentheses can be extracted from the target string.
For example:
/ [amurz] +\ dletters / retrieve one or more lowercase letters followed by one or more numbers
If we care about the number at each trailing end, we can put the number part of the pattern in parentheses (/ [a murz] + (\ d) /) so that we can extract the number from the retrieved match.
Function 3: allow the introduction of previous subexpressions at the end of the same regular expression
How to do this: add one or more digits after the character\ (this number specifies the position of the parenthesized subexpression in the regular expression)
For example:\ 1 refers to the first parenthesized subexpression
Note: the position is subject to the position of the left parenthesis participating in the count.
Regular expressions do not allow single quotation marks in content enclosed in double quotes and vice versa.
Summary:
| | Select, which matches the subexpression on the left or right of the character |
(.) To combine several items into a single unit.
(.?) Only combine, combine items into a unit, but do not remember the characters that match the group
\ n matches the characters matched for the first time in the nth group. the group is a subexpression in parentheses, and the group index is the number of left parentheses from left to right. (?: this form of grouping does not participate in index coding
4. Specify the matching location:
Anchor: specify the legal location where the match occurs
^ matches the beginning of the string
$matches the end of the string
\ b matches the boundary of a word (between\ w and\ W)
\ B match the position of the non-word boundary
5. Modifiers
Modifiers are placed outside / / and do not appear between two / lines
I is not case sensitive
G global match, find all matches, rather than stop after finding the first one
M multi-line matching
6. Methods of String objects for pattern matching
Method 1: search () retrieves the matching location
Parameters: a regular expression
Return: the starting position of the first matching substring. If no matching substring is found,-1 is returned.
For example:
"javascript" .search (/ script/i); / / return value is 4
Note: ① if the argument to search () is not a regular expression, it will be converted to a regular expression through the RegExp constructor
② search () does not support global retrieval
Method 2: replace () performs retrieval and replacement operations
Parameter: first-- > regular expression
The second-- > string to replace (or a function that dynamically calculates the replacement string)
Note: ① supports g
G is set in the regular expression: all substrings that match the pattern will be replaced with the string specified by the second parameter
G is not set in the regular expression: only replace the first substring that matches
② if the first argument is not regular, but a string, replace () will search for that string directly.
For example: replace all case-insensitive javascript with case-correct JavaScript
Var str = "javascript,javascript,javascript,javaScript"; alert (str.replace (/ javascript/gi, "JavaScript"); / / JavaScript,JavaScript,JavaScript,JavaScript
Method 3: match ()
Parameters: regular expression
Return: an array of matching results
Support g
Example 1:
Var math = "1 plus 2 equals 3" .match (/\ dbadbadg); console.log (math); / / ["1", "2", "3"] console.log (typeof math); / / object
Example 2:
Var math = "1 plus 2 equals 3" .match (/\ d /); console.log (math); / / ["1", index: 0, input: "1 plus 2 equals 3"] console.log (typeof math); / / object
Note:
Pass a non-global regular expression to the math () of the string:
Match: only the first match is retrieved
Return value: array
The first element of the array: the matching string
The second element of the array: a subexpression in parentheses in a regular expression that returns an array with two attributes-- > index and input
Method 4: split ()
Return value: array
Delimiter: parameter of split ()
For example:
Var str= "1, 2, 3, 4, 5, 6, 7, 8". Split (','); console.log (str); / / ["1", "2", "3", "4", "5", "6", "7", "8"]
When the parameter is a regular expression (you can specify a delimiter to allow as many spaces on both sides as you want)
Return: the string used to call it is split into an array of substrings
Var str= "1, str 2, 3, 4, 4, 5, 5, 6, 7, 8". Split (/\ blank,\ blank /); / / allow any number of spaces on both sides to be left console.log (str); / / ["1", "2", "3", "4", "5", "6", "7", "8"]
7. RegExp object
The RegExp () constructor:
Parameters: two string parameters
First: the body of the regular expression (the text between the two slashes)
The second: (optional) specifies the modifier of the regular expression (or a combination of these three)
Note: both string literals and regular expressions use the / character as the prefix for the translated character.
For example:
Var zipCode = new RegExp ('\ d {5}','g'); / / globally matches the five digits in the string. Note that this is / / rather than / above is all the content of the article "sample Analysis of js regular expressions". 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.
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.