In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the JS basic series of regular expression example analysis, the article is very detailed, has a certain reference value, interested friends must read it!
1.1 what is a regular expression
Regular expressions (regular expression) are objects that describe character patterns. ECMAScript's RegExp class represents regular expressions, while both String and RegExp define functions that use regular expressions for powerful pattern matching and text retrieval and replacement.
Regular expressions are used for string pattern matching and retrieval substitution, and are a powerful tool for performing pattern matching on strings.
1.2 the role of regular expressions
Regular expressions are mainly used to validate the input data of the client.
After the user completes the form and clicks the button, the form is sent to the server, where it is usually further processed with server scripts such as PHP, ASP.NET, JSP, and so on. Because of client-side authentication, it can save a lot of server-side system resources and provide a better user experience.
Second, create a regular expression = (123) = =
To use a regular expression, you must first create a regular expression object. There are two ways to create an object:
2.1Mode 1: create using the keyword new
Var patt = new RegExp (pattern,modifiers)
Parameter 1: the pattern of the regular expression. String form
Parameter 2: mode modifier. Used to specify global matching, case-sensitive matching, and multiline matching
/ * created a regular expression parameter 1: pattern is: girl, which means that string parameters such as "girl" can be matched: pattern modifier: gi g represents global matching I represents case-insensitive * / var pa = new RegExp ("girl", "gi"); / / Test whether the string "Hello my girl" in the parameter matches the matching pattern. Var isExist = pa.test ("Hello my girl"); / / in this case, it matches, this string contains girl, so true alert (isExist) is returned; / / true
2.2 Mode 2: use regular expressions to directly measure
Var pa = / pattern/modifiers
The pattern between two / represents a regular expression, followed by a pattern modifier after the last /
For example, the above example can write var pa = / girl/gi
Note: at this time, both the pattern and the pattern modifier can add double or single quotation marks
Var pa = / girl/gi; alert (pa.test ("awesome my girl"); / / true
Regular expression pattern modifier = (126) = =
There are three schema modifiers in JavaScript: g i u
G: represents the global situation. It means that a string will be matched multiple times. If you do not write g, you will only match once. Once the match is successful, you will not match again.
I: indicates that case is ignored. It means that it is not case-sensitive when matching.
U: indicates that multiple lines can be matched.
Fourth, the detailed explanation of the regular expression method = (127) =
The frequently used regular expression methods are test () and exec ().
4.1 test () method
Test (string)
Parameter: string to match
Return value: true for successful match and false for failure
This method is convenient when you only want to know whether the target string matches a pattern, but you don't need to know its text content. Therefore, the test () method is often used in if statements.
Var pa = / girl/gi; if (pa.test ("my girl")) {alert ("this girl is perfect for you");} else {alert ("you are destined to have no girl to match");}
4.2 exec () method
Exec (string): this method is designed specifically for capturing groups
Parameter: string to match
Return value: an array is returned. Return null if there is no match
Instructions for returning an array of values:
It is indeed an instance of Array.
But this array has two additional properties: index and input
Index: indicates the index of the matching string in the source string
Input: represents a matching source string.
The first item of the array is the string that matches the entire pattern, and the other items are strings that match the capture group in the pattern.
If there is no capture group, there is only the first item in the array. We'll talk about the concept of capture group later.
Var pa = / girl/gi; var testStr = "myGirl, yourgirl, hisgIrl"; var girls = pa.exec (testStr); / / capture alert (girls.length + ":" + (girls instanceof Array)); / / regular expression has no capture group, so the array length is 1 alert (girls [0]) / / the Girl is captured for the first time / / since we are using a global match, this match continues to match alert (pa.exec (testStr) [0]); / / girl alert (pa.exec (testStr)); / / gIrl alert (pa.exec (testStr)). / / there is no matching string to continue backward, so return null / / return null. If you continue to match again, it will go back to the beginning of the string and rewrite to start the match. Alert (pa.exec (testStr)); / / Girl / /. Start a new round of matching
So if we want to find all the matching strings, we can use a loop when we want to find all the matching strings, and the end condition is that the matching result is null.
Var pa = / girl/gi; var testStr = "myGirl, yourgirl, hisgIrl"; var girls; while (girls = pa.exec (testStr)) {/ / if equal to null, it automatically changes to false and ends. Alert (girls);}
Divide into groups. Enclosing tasks in regular expressions with () is a group. Groups can be nested.
The content in / / () is group 1 (Girl). In fact, we can see that group 0 girl (Girl) / / corresponds to the subscript of the matching result array in the future. Var pa = / girl (Girl) / gi; var test = "girlGirl abdfjla Girlgirl fal girl"; var girls; while (girls = pa.exec (test)) {/ / after matching, the 0th element of the array corresponds to the match result of the 0th group, and the first element corresponds to the matching result of the first group for (var I = 0; I
< girls.length; i++) { console.log(girls[i]); } console.log("-------------"); }//最终运行结果:girlGirlGirl-------------Girlgirlgirl------------ 五、正则表达式规则==(124)== 表达式规则 正则表达式元字符是包含特殊含义的字符。它们有一些特殊功能,可以控制匹配模式的 方式。反斜杠后的元字符将失去其特殊含义。 字符类:单个字符和数字 [0-9A-Za-z] 元字符/元符号 匹配情况 . 匹配除换行符外的任意字符 [a-z0-9] 匹配括号中的字符集中的任意字符 [^a-z0-9] 匹配任意不在括号中的字符集中的字符 \d ==[0-9] 匹配数字 \D ==[^0-9] 匹配非数字,同[^0-9]相同 \w [0-9A-Za-z_] 匹配字母和数字及_ \W 匹配非(字母和数字及_) 字符类:空白字符 元字符/元符号 匹配情况 \0 匹配null 字符 \b 匹配空格字符 \n 匹配换行符 \r 匹配回车字符 \t 匹配制表符 \s 匹配空白字符、空格、制表符和换行符 \S 匹配非空白字符 字符类:锚字符 元字符/元符号 匹配情况 ^ 行首匹配 $ 行尾匹配 字符类:重复字符 元字符/元符号 匹配情况 ? 例如(x?) 匹配0个或1 个x * 例如(x*) 匹配0个或任意多个x + 例如(x+) 匹配至少一个x (xyz)+ 匹配至少一个(xyz) {m,n} 例如x{m,n} n>= times > = m matches a minimum of m and a maximum of n x
{n} match the previous term n times
{n,} match the previous term n times or multiple times
6. Common regular expression = (128) =
1. Check the zip code
Var pattern = / [1-9] [0-9] {5} /; / / A total of 6 digits, the first digit cannot be 0var str = '224000 alert (pattern.test (str))
2. Check the file package
Var pattern = / [\ w] +\ .zip | rar | gz/; / /\ w indicates that all numbers and letters are underlined var str = '123.zipletters; / /\. Indicates a match, followed by a selection of alert (pattern.test (str))
3. Delete extra spaces
Var pattern = /\ sUnig; / / g must be global in order to match all var reg=new RegExp ('\\ smatching grammar grammar g'); var str = '1112222333' matching str.replace result = str.replace (pattern,''); / / matching spaces to blank alert (result)
4. Delete spaces
Var pattern = / ^\ s hammer; var str = 'goo gle'; alert (str+ "" + str.length); var result = str.replace (pattern,''); alert (result+ "" + result.length); pattern = /\ s automobile raceme; result = result.replace (pattern,'); alert (result+ "" + result.length); pattern = /\ s hammer; result = result.replace (pattern,'); alert (result+ "+ result.length) 5. Simple email verification var pattern = / ^ ([a-zA-Z0-9 _\.\ -] +) @ ([a-zA-Z0-9 _\.\ -] +)\. ([a-zA-Z] {2jin4}) $/; var str = 'yc60.com@gmail.com';alert (pattern.test (str)) Var pattern = / ^ ([\ w\.\ -] +) @ ([\ w\.\ -] +)\. ([\ w] {2heli4}) $/; var str = 'yc60.com@gmail.com';alert (pattern.test (str))
String method that supports regular expressions
Method describes how search retrieves the index of the first match that matches the regular expression. Match found a match for one or more regular expressions. Replace replaces the substring that matches the regular expression. Split splits a string into an array of strings. Var s = "Abc123aBc"; alert (s.search (/ abc/gi)); alert (s.search (/ abc/gi)); / / even if the global mode is set, each search looks for the / / match method backward from the beginning and the exec () method of the regular expression has the same effect, but match will put all matches in one array at once and return all alert (s.match (/ abc/gi)). / Abc,aBc alert (s.replace (/ [ab] / gi, "x")); / replace an or b with x var ss = s.split (/ [0-9] + / gi); / / cut with one or more digits. Abc,aBc alert (ss); this is all of the article "sample Analysis of regular expressions in the JS Foundation Series". Thank you for reading! Hope to share the content to help you, more related 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.