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 how to use JS regular expressions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
/ / check whether it is made up of numbers.
Funtin isigit (s) {var patrn=/ ^ [0-9] {1 patrn.x 20} $/; if (! patrn.x (s)) return fals return true}
The JavaSript form validates mail and determines whether an input is a mailbox mail, which is achieved by regular expressions.
/ / check mail mailbox funtin ismail (str) {var rg = / ^ ([a-zA-Z0-9 letters -]) + @ ([a-zA-Z0-9 letters -]) + ((\. [a-zA-Z0-9 seconds -] {2 zA-Z0 3}) {1 str 2}) $/; return rg.test (str);} / verify login names: only 5-20 logins can be entered, starting with a letter, with numbers, "_", "." The string funtin isRgistrUsrNam (s) {var patrn=/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4 var patrn=/ 19} $/; if (! patrn.x (s)) rturn fals return true} / / verify the user name: only 1-30 letters can be entered for funtin isTruNam (s) {var patrn=/ ^ [a-zA-Z] {1pm 30} $/ If (! patrn.x (s)) rturn fals return true} / / check password: you can only enter 6-20 letters, numbers, and underscores funtin isPassw (s) {var patrn=/ ^ (\ w) {6pm 20} $/ If (! patrn.x (s)) rturn fals return true} / / verify ordinary telephone and fax numbers: you can start with "+" and contain "-" funtin isTl (s) {/ / var patrn=/ ^ [+] {0Mague 1} (\) {1Mague 3} []? ([-]? (\) {1Magol 12}) + $/ Var patrn=/ ^ [+] {0patrn.x 1} (\) {1meme3} []? ([-]? ((\) | []) {1cm12}) + $/; if (! patrn.x (s)) return fals return true} / / verify the mobile phone number: it must start with a number and may contain "-" funtin isMbil (s) {var patrn=/ (s) {0ct 1} (\) {1jue 3} []? ([-]? (\) | []) {1pm 12}) + $/ If (! patrn.x (s)) return fals return true} / / check Postal Code funtin isPstal (s) {/ / var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; if (! patrn.x (s)) return fals return true} / / verify the search keyword funtin isSarh (s) {var patrn=/ ^ [^ `~! @ # $% ^ & * () + = |\\] [\]\ {\}: '\,. /?] {1} [^ `~! @ $% ^ & () + = |\\] [\]\]\ {{\}:;'\,.] {0rturn fals rturn tru 19} $/; if (! patrn.x (s)) rturn fals rturn tru} funtin isIP (s) / / by zrgling {var patrn=/ ^ [0-9.] {1pm 20} $/; if (! patrn.x (s)) return fals return true}
Regular expression
"^\ + $"
/ / non-negative integer (positive integer + 0) ^ [0-9] * [1-9] [0-9] * $"/ / positive integer" ^ (-\ +) | (0 +) $"/ / non-positive integer (negative integer + 0) ^-[0-9] * [1-9] [0-9] * $" / / negative integer "^ -? + $" / integer "^\ + (\.\ +)? $" / / non-negative floating point (positive floating point + 0) "^ ([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) Positive floating point numbers ^ (-\\ + (\\.\\ +)?) | (0 + (\\ .0 +)) $"/ / non-positive floating point numbers (negative floating point numbers + 0)" ^ (0-9) +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) -9] * [1-9] [0-9] *)) $"/ / negative floating point number" ^ (-?\ +) (\.\ +)? $"/ / floating point number" ^ [A-Za-z] + $"/ 26-letter string" ^ [Amurz] + $"/ string consisting of 26 letters of capitalization" ^ [Amurz] + $" / / A string of 26 lowercase letters "^ [A-Za-z0-9] + $" / / a string of numbers and 26 letters "^\\ alphabetic $" / / composed of numbers, A string consisting of 26 English letters or underscores "^ [\\ w -] + (\\ .[\\ w -] +) * @ [\\ w -] + (\\ .[\\ w -] +) + $" / / mail address "^ [a-zA-z] +: / / (\\ w+ (-\\ w+) *) (\. (\\ w+ (-\\ w+) *)) * (\\?\\ S *)? $"/ / url" ^ [A-Za-z0-9 _] * $"
Regular expressions use detailed explanation
Brief introduction
In a nutshell, regular expressions are a powerful tool for pattern matching and substitution. Its functions are as follows:
Test a pattern of the string.
For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string.
This is called data validation.
Replace the text. You can use a regular expression to identify specific text in a document, and then you can delete it all or replace it with other text.
Extract a substring from the string based on pattern matching. Can be used to find specific text in text or input fields.
Basic grammar
After we have a preliminary understanding of the function and function of regular expressions, let's take a specific look at the grammatical format of regular expressions.
Regular expressions generally take the following form:
/ lv/
The part between the "/" delimiters is the pattern that will be matched in the target object. Users only need to put the content of the pattern that they want to find a match between the "/" delimiters. In order to give users more flexibility to customize pattern content, regular expressions provide special "metacharacters". Metacharacters refer to those special characters with special meaning in regular expressions, which can be used to define the occurrence pattern of their leading characters (that is, characters in front of metacharacters) in the target object.
The more commonly used metacharacters include "+", "*", and "?".
The "+" metacharacter states that its leading character must appear one or more times in a row in the target object.
The "*" metacharacter states that its leading character must appear zero or multiple times in the target object.
"?" The metacharacter states that its leading object must appear zero or once in a row in the target object.
Next, let's take a look at the specific application of regular expression metacharacters.
Because the above regular expression contains a "+" metacharacter, it can match strings such as "fl", "f", or "ftball" in the target object that appear one or more letters after the letter f.
Because the above regular expression contains the "*" metacharacter, it can match the string of "asy", "g", or "gg" in the target object in which zero or more letters appear after the letter.
/ Wil?/ because the above regular expression contains "?" Metacharacters that can match "Win" or "Wilsn" in the target object, such as a string with zero or one letter l after the letter I.
Sometimes I don't know how many characters to match. In order to adapt to this uncertainty, regular expressions support the concept of qualifiers. These qualifiers can specify how many times a given component of a regular expression must appear to satisfy a match.
{n} n is a non-negative integer. Match the determined n times. For example,'{2} 'does not match''in'Bb', but can match two of'f'.
{n,} n is a non-negative integer. Match at least n times. For example,'{2,} 'does not match''in'Bb', but can match all in'f'. {1,}'is equivalent to'+'. {0,}'is equivalent to'*'.
{n ·m} m} m and n are non-negative integers, where n = 5.5) {/ / test the version of JSript. Var sr = "Th rain in Spain falls mainly in th plain."; var r = /\ w regular expression pattern is created. Var arr; whil ((arr = r.x (sr))! = null) umnt.writ (arr.inx + "-" + arr.lastInx + arr + "\ t");} ls {alrt ("Please use a newer version of JSript");}}
Return value: 0-3Th 4-8rain 9-11in 12-17Spain 18-23falls 24-30mainly 31-33in 34-37th 38-43plain
Tst method
\\ returns a Blan value indicating whether a pattern exists in the string being looked up.
Rgxp.tst (str)
Parameter\\
Rgxp
Required. A regular expression object that contains regular expression patterns or available flags.
Str
Required. The string on which to test the lookup.
Description
The tst method checks to see if a pattern exists in the string, returns tru if it does, and returns fals otherwise.
The properties of the global Rgxp object are not modified by the tst method.
Example
The following example illustrates the use of the tst method:
Funtin Tstm (r, s) {var S1; / / declare variables. / / check whether a regular expression exists in the string. If (r.tst (s)) / / Test whether it exists. S1 = "ntains"; / / s contains the pattern. Ls S1 = "s nt ntain"; / / s does not contain a pattern. Rturn ("" + s + "+ S1 +" + r.sur + "); / / returns the string. }
Function call: umnt.writ (Tstm (/ ain+/, "Th rain in Spain falls mainly in th plain."))
Return value:'Th rain in Spain falls mainly in th plain.' Ntains' ain+'
Math method
Use the regular expression pattern to perform a lookup on a string and return the results containing the lookup as an array.
\\ stringbj.math (rgxp)
Parameters.
\\ stringbj is required. The String object or string text to find.
Rgxp
Required. Is a regular expression object that contains regular expression patterns and available flags. It can also be a variable name or string literal that contains regular expression patterns and available flags.
Description
\\ if the math method does not find a match, it returns null. Returns an array if a match is found and updates the properties of the global Rgxp object to reflect the match result.
The array returned by the math method has three properties: input, inx, and lastInx. The Input attribute contains the entire searched string. The Inx property contains the location of the substring that matches in the entire searched string. The LastInx attribute contains the next position of the last character in the last match.
If the global flag (g) is not set, the 0 element of the array contains the entire match, while elements 1 to n contain any submatches that have ever occurred in the match. This is equivalent to the x method with no global flag set. If the global flag is set, elements 0 to n contain all matches.
Example\
The following example demonstrates the use of the math method: funtin Mathm () {var r, r; / declare variables. Var s = "Th rain in Spain falls mainly in th plain"; r = / ain/i; / / create a regular expression pattern. R = s.math (r); / / try to match the search string. Rturn (r); / / returns the place where "ain" first appears. } return value: ain this example shows the use of the math method with g flag setting. Funtin Mathm () {var r, r; / / declare variables. Var s = "Th rain in Spain falls mainly in th plain"; r = / ain/ig;// creates a regular expression pattern. R = s.math (r); / / try to match the search string. The array returned by rturn (r) / / contains all four matches that appear in "ain" / /. } return value: ain,ain,ain,ain
The above lines of code demonstrate the use of the math method for string literals.
Var r, r = "Spain"; r = "Th rain in Spain" .rpla (r, "anaa"); return r
Return value: Th rain in anaa
Sarh method
Returns the location of the first substring that matches what the regular expression is looking for.
Stringbj.sarh (rgxp)
Parameters.
\\ stringbj
Required. The String object or string literal on which to find.
Rgxp
Required. A regular expression object that contains regular expression patterns and available flags.
Description
The sarh method indicates whether there is a corresponding match. If a match is found, the sarh method returns an integer value indicating the offset of the match from the beginning of the string. If no match is found,-1 is returned.
Example\\
The following example demonstrates the use of the sarh method.
Funtin Sarhm () {var r, r; / / declare variables. Var s = "Th rain in Spain falls mainly in th plain."; r = / falls/i; / / create a regular expression pattern. R = s.sarh (r); / / find the string. Rturn (r); / / returns the Blan result. }
Return value: 18
Regular expression syntax
A regular expression is a text pattern consisting of ordinary characters (such as characters a to z) and special characters (called metacharacters). The pattern describes one or more strings to be matched when finding the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.
Here are some examples of regular expressions that you might encounter:
JSript VBSript match / ^\ [\ t] * $/ "^\ [\ t] * $" matches a blank line. /\ {2} -\ {5} / "\ {2} -\ {5}" verify that an I number consists of a 2-digit number, a hyphen, and a 5-digit number. /. * / ". *" matches a HTML tag.
The following table is a complete list of metacharacters and their behavior in the context of regular expressions:
Character description
\ Mark the next character as a special character, or a literal character, or a backward reference, or an octal escape character. For example,'n 'matches the character "n".' \ n' matches a newline character. The sequence'\ 'matches "and" ("matches" (".
^ matches the starting position of the input string. If the Multilin property of the Rgxp object is set, ^ also matches the position after'\ n'or'\ r'.
$matches the end of the input string. If the Multilin property of the Rgxp object is set, $also matches the position before'\ n'or'\ r'.
* matches the previous subexpression zero or more times. For example, z* can match "z" and "z". * is equivalent to {0,}.
+ matches the previous subexpression one or more times. For example,'z 'can match "z" and "z", but not "z". + is equivalent to {1,}.
? Matches the previous subexpression zero or once. For example, "(s)?" Can match the "" in "" or "s". ? It is equivalent to {0jue 1}.
{n} n is a non-negative integer. Match the determined n times. For example,'{2} 'does not match''in'Bb', but can match two of'f'.
{n,} n is a non-negative integer. Match at least n times. For example,'{2,} 'does not match''in'Bb', but can match all in'f'. {1,}'is equivalent to'+'. {0,}'is equivalent to'*'.
{n ·m} m} m and n are non-negative integers, where n
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.