In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is RegExp? in view of this question, this article introduces in detail the corresponding analysis and answers, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
What is RegExp?
RegExp is an abbreviation for regular expression (Regular expression) and is used to perform pattern matching on strings.
Commonly used for format verification, regular replacement, finding substrings, etc.
The regular expressions of various programming languages are basically the same, and different languages may have some small differences.
RegExp syntax
1.Compact / directly instantiate
2.var reg = new RegExp (pattern [, flags])
3.Compact / implicitly create (recommended)
4.var reg = / pattern/flags
The parameter pattern is a string that specifies the pattern of the regular expression or other regular expression.
The parameter [, flags] is an optional string containing the attributes "g" (global), "I" (ignoreCase), and "m" (multiline).
Before ECMAScript standardization, the m attribute was not supported. If pattern is a regular expression, not a string, you must omit this parameter.
Concepts: subexpression
In a regular expression, the content enclosed in parentheses is a subexpression, and the content matched by the subexpression is captured by the system into a buffer, using\ n (n: number) to reverse reference the contents of the system's buffer No. N.
Scenario: the latter content is required to be the same as the previous one, and you can use subexpressions
1.Plus / find four consecutive identical digits
2.var str = "1212ab45677778cd"
3.var reg = / (\ d)\ 1\ 1\ 1/gi
4.console.log (str.match (reg))
5.// OUTPUT:7777
Concept: square brackets (character cluster)
1.var str = "Is this all there is?"
2.var patt1 = / [a murh] / g
3. [XSS _ clean] (str.match (patt1))
4.// OUTPUT:h,a,h,e,e
Square bracket action
Square brackets act as [abc] to find any characters between square brackets. [^ ABC] look for any characters that are not between square brackets. [0-9] look for any number from 0 to 9. Look for any character from lowercase a to lowercase z with\ d [a murz]. [Amurz] looks for any character from uppercase A to uppercase Z. [Amurz] looks for any character from uppercase A to lowercase z. [0-9a-zA-Z] search for 0-9 quotient aLizMagee Amurz
Concept: metacharacter
Metacharacters (Metacharacter) are characters with a special meaning:
Metacharacter | function
-|-
|\ | escape character (,), /,\ |
| Select a match, which can match multiple rules |
. | find a single character, except newline and line Terminator.
\ w | look for word characters. Characters (letters, numbers, underscores _)
\ W | find non-word characters.
\ d | find the number.
\ d | find non-numeric characters.
\ s | look for white space characters. Space
\ s | look for non-white space characters.
\ b | match word boundaries.
\ B | matches non-word boundaries.
\ 0 | find NUL characters.
\ n | find newline characters.
\ f | find the feed character.
\ r | find carriage return.
\ t | find tabs.
\ v | find vertical tabs.
\ xxx | find the characters specified by the octal number xxx.
\ xdd | find the characters specified in the hexadecimal number dd.
\ uxxxx | find the Unicode character specified in the hexadecimal number xxxx.
Concept: quantifier
The quantifier function n + matches any string that contains at least one n. Match any string containing zero or more ns with {1,} n *. Same as {0,} n? Matches any string that contains zero or one n. Matches a string containing a sequence of X n with {0jue 1} n {X}. N {XQuery Y} matches a string containing a sequence of X to Y n. N {X,} matches a string containing a sequence of at least X n. N $matches any string that ends with n. ^ n matches any string that starts with n. Note that / [^ a] / and / ^ [a] / are different. The former is excluded and the latter represents the first place. (? = n) matches any string followed by the specified string n. Forward preview (?! n) matches any string that is not followed by the specified string n. Reverse pre-check
RegExp object method
Test ()
The test () method retrieves whether the specified value exists in the string. The return value is true or false.
1.var patt1 = new RegExp ('e')
2.console.log (patt1.test ('some text'))
3.// OUTPUT:true
4.var patt2 = new RegExp ('ee')
5.console.log (patt2.test ('some text'))
6.// OUTPUT:false
1. Whether it is a QQ number or not.
2. The first place of a / 1 cannot be 0 ^ [1-9]
3.Compact / 2 must be a number of [5,11] digits\ d {4,9}
4.var str = '80583600'
5.var regexp = / ^ [1-9] [0-9] {4pm 10} $/ gim
6.if (regexp.test (str)) {
7. Alert ('is')
8.} else {
9. Alert ('no')
10.}
Exec ()
The exec () method retrieves the specified value in the string. The return value is the value found. If no match is found, null is returned.
1.var patt1 = new RegExp ('e')
2.console.log (patt1.exec ('some text'))
3.// OUTPUT:e
4.var patt2 = new RegExp ('ee')
5.console.log (patt2.exec ('some text'))
6.// OUTPUT:null
Compile ()
Compile () can either change the retrieval mode or add or remove a second parameter.
1.var patt1=new RegExp ("e")
2. [XSS _ clean] (patt1.test ("The best things in life are free")); / / true
3.Compact / changed the retrieval mode
4.patt1.compile ("eee")
5. [XSS _ clean] (patt1.test ("The best things in life are free")); / / false
Methods of String objects that support regular expressions
Search retrieves values that match the regular expression.
1.var str = "Visit W3School!"
2.console.log (str.search (/ W3School/))
3.// OUTPUT:6
Match found a match for one or more regular expressions.
1.var str= "1 plus 2 equal 3"
2.console.log (str.match (/\ dhammerg))
3.// OUTPUT:1,2,3
Replace replaces the substring that matches the regular expression.
1.var str = "Visit Microsoft!"
2.console.log (str.replace (/ Microsoft/, "W3School"))
3.// OUTPUT:Visit W3School!
1. The number of characters with the largest number of characters to find duplicates
2.var str = 'g21ss4aebaqiersb43sgnnsstht6sss60snnsj8resw0roomssss'
3.Compact / split: converts a string to an array
4.Compact / sort: sort the array, ASCII
5.Compact / join: converts an array to a string
6.var str_new = str.split (''). Sort (). Join ('')
7. [XSS _ clean] (str +'
')
8. [XSS _ clean] (str.split ('') +'
')
9. [XSS _ clean] (str.split (''). Sort () +'
')
10. [XSS _ clean] (str.split (''). Sort (). Join ('') +'
')
11. Use / match the character and repeat the character at least once.
12.var regexp = / (\ w)\ 1scratch _ g
13.var index = 0
14.var value =''
15.str_new.replace (regexp, function ($0, $1) {
16. / / [xss_clean] ($0)
17. / / [xss_clean] ($1)
18. If (index
< $0.length) { 19. index = $0.length; 20. value = $1; 21. } 22.}); 23.[xss_clean]('重复项最多的字符是:' + value + ',个数是:' + index); 24.// OUTPUT: 25.// 0012344668__aabbeeegghjnnnnrrssssssssssssssssttw 26.// 重复项最多的字符是:s,个数是:16 split 把字符串分割为字符串数组。 1.var str = "How are you doing today?" 2.[xss_clean](str.split(/\s+/)); 3.// OUTPUT:How,are,you,doing,today? 经验: 检验格式(邮箱格式、IP格式)是否正确,用test() 抓取星期(如所有手机号),用exec()、match() 替换敏感词汇,用replace() 常见的 正则表达式 校验 1.// 常见的 正则表达式 校验 2.// QQ号、手机号、Email、是否是数字、去掉前后空格、是否存在中文、邮编、身份证、URL、日期格式、IP 3.var myRegExp = { 4. // 检查字符串是否为合法QQ号码 5. isQQ: function(str) { 6. // 1 首位不能是0 ^[1-9] 7. // 2 必须是 [5, 11] 位的数字 \d{4, 9} 8. var reg = /^[1-9][0-9]{4,9}$/gim; 9. if (reg.test(str)) { 10. console.log('QQ号码格式输入正确'); 11. return true; 12. } else { 13. console.log('请输入正确格式的QQ号码'); 14. return false; 15. } 16. }, 17. // 检查字符串是否为合法手机号码 18. isPhone: function(str) { 19. var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57]|17[678])[0-9]{8}$/; 20. if (reg.test(str)) { 21. console.log('手机号码格式输入正确'); 22. return true; 23. } else { 24. console.log('请输入正确格式的手机号码'); 25. return false; 26. } 27. }, 28. // 检查字符串是否为合法Email地址 29. isEmail: function(str) { 30. var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 31. // var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 32. if (reg.test(str)) { 33. console.log('Email格式输入正确'); 34. return true; 35. } else { 36. console.log('请输入正确格式的Email'); 37. return false; 38. } 39. }, 40. // 检查字符串是否是数字 41. isNumber: function(str) { 42. var reg = /^\d+$/; 43. if (reg.test(str)) { 44. console.log(str + '是数字'); 45. return true; 46. } else { 47. console.log(str + '不是数字'); 48. return false; 49. } 50. }, 51. // 去掉前后空格 52. trim: function(str) { 53. var reg = /^\s+|\s+$/g; 54. return str.replace(reg, ''); 55. }, 56. // 检查字符串是否存在中文 57. isChinese: function(str) { 58. var reg = /[\u4e00-\u9fa5]/gm; 59. if (reg.test(str)) { 60. console.log(str + ' 中存在中文'); 61. return true; 62. } else { 63. console.log(str + ' 中不存在中文'); 64. return false; 65. } 66. }, 67. // 检查字符串是否为合法邮政编码 68. isPostcode: function(str) { 69. // 起始数字不能为0,然后是5个数字 [1-9]\d{5} 70. var reg = /^[1-9]\d{5}$/g; 71. // var reg = /^[1-9]\d{5}(?!\d)$/; 72. if (reg.test(str)) { 73. console.log(str + ' 是合法的邮编格式'); 74. return true; 75. } else { 76. console.log(str + ' 是不合法的邮编格式'); 77. return false; 78. } 79. }, 80. // 检查字符串是否为合法身份证号码 81. isIDcard: function(str) { 82. var reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/; 83. if (reg.test(str)) { 84. console.log(str + ' 是合法的身份证号码'); 85. return true; 86. } else { 87. console.log(str + ' 是不合法的身份证号码'); 88. return false; 89. } 90. }, 91. // 检查字符串是否为合法URL 92. isURL: function(str) { 93. var reg = /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 94. if (reg.test(str)) { 95. console.log(str + ' 是合法的URL'); 96. return true; 97. } else { 98. console.log(str + ' 是不合法的URL'); 99. return false; 100. } 101. }, 102. // 检查字符串是否为合法日期格式 yyyy-mm-dd 103. isDate: function(str) { 104. var reg = /^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9]$/; 105. if (reg.test(str)) { 106. console.log(str + ' 是合法的日期格式'); 107. return true; 108. } else { 109. console.log(str + ' 是不合法的日期格式,yyyy-mm-dd'); 110. return false; 111. } 112. }, 113. // 检查字符串是否为合法IP地址 114. isIP: function(str) { 115. // 1.1.1.1 四段 [0 , 255] 116. // 第一段不能为0 117. // 每个段不能以0开头 118. // 119. // 本机IP: 58.50.120.18 湖北省荆州市 电信 120. var reg = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/gi; 121. if (reg.test(str)) { 122. console.log(str + ' 是合法的IP地址'); 123. return true; 124. } else { 125. console.log(str + ' 是不合法的IP地址'); 126. return false; 127. } 128. } 129.} 130.// 测试 131.// console.log(myRegExp.isQQ('80583600')); 132.// console.log(myRegExp.isPhone('17607160722')); 133.// console.log(myRegExp.isEmail('80583600@qq.com')); 134.// console.log(myRegExp.isNumber('100a')); 135.// console.log(myRegExp.trim(' 100 ')); 136.// console.log(myRegExp.isChinese('baixiaoming')); 137.// console.log(myRegExp.isChinese('小明')); 138.// console.log(myRegExp.isPostcode('412345')); 139.// console.log(myRegExp.isIDcard('42091119940927001X')); 140.// console.log(myRegExp.isURL('https://www.baidu.com/')); 141.// console.log(myRegExp.isDate('2017-4-4')); 142.// console.log(myRegExp.isIP('1.0.0.0')); 一、校验数字的表达式 1.数字:^[0-9]*$ 2.n位的数字:^\d{n}$ 3.至少n位的数字:^\d{n,}$ 4.m-n位的数字:^\d{m,n}$ 5.零和非零开头的数字:^(0|[1-9][0-9]*)$ 6.非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$ 7.带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$ 8.正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$ 9.有两位小数的正实数:^[0-9]+(.[0-9]{2})?$ 10.有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$ 11.非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$ 12.非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$ 13.非负整数:^\d+$ 或 ^[1-9]\d*|0$ 14.非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$ 15.非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ 16.非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ 17.正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ 18.负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ 19.浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ 二、校验字符的表达式 1.汉字:^[\u4e00-\u9fa5]{0,}$ 2.英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$ 3.长度为3-20的所有字符:^.{3,20}$ 4.由26个英文字母组成的字符串:^[A-Za-z]+$ 5.由26个大写英文字母组成的字符串:^[A-Z]+$ 6.由26个小写英文字母组成的字符串:^[a-z]+$ 7.由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$ 8.由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$ 9.中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$ 10.可以输入含有^%&',;=?$\"等字符:[^%&',;=?$\x22]+ 11.禁止输入含有~的字符:[^~\x22]+ 三、特殊需求表达式 1.Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ 2.域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.? 3.InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ 4.手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$ 5.电话号码("XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$ 6.国内电话号码(0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7} 7.身份证号(15位、18位数字):^\d{15}|\d{18}$ 8.短身份证号码(数字、字母x结尾):^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$ 9.帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 10.密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$ 11.强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 12.日期格式:^\d{4}-\d{1,2}-\d{1,2} 13.一年的12个月(01~09和1~12):^(0?[1-9]|1[0-2])$ 14.一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$ 15.xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$ 16.中文字符的正则表达式:[\u4e00-\u9fa5] 17.双字节字符:[^\x00-\xff] (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)) 18.空白行的正则表达式:\n\s*\r (可以用来删除空白行) 19.HTML标记的正则表达式:]*>. *? | (the version circulated on the Internet is too bad, the above one is only partial, and there is nothing I can do about complex nested tags.)
20. Regular expression of leading and trailing white space characters: ^\ swords $or (^\ s*) | (\ sblank $) (can be used to delete white space characters (including spaces, tabs, page feeds, etc.) at the beginning and end of a line, a very useful expression)
21. Tencent QQ number: [1-9] [0-9] {4,} (Tencent QQ number starts from 10000)
twenty-two。 China Postal Code: [1-9]\ d {5} (?!\ d) (China Postal Code is 6 digits)
23.IP address:\ d +\.\ d +\.\ d + (useful when extracting IP addresses)
24.IP address: (?: 25 [0-5] | 2 [0-4]\ d | [01]?\\ d?\\ d)\\.) {3} (?: 25 [0-5] | 2 [0-4]\ d | [01]?\ d?\\ d)
The input format of the money:
1.1. There are four forms of money we can accept: "10000.00" and "10000.00", and "10000" and "10000" without "points": ^ [1-9] [0-9] * $
2.2. This means any number that does not start with 0, but it also means that a character "0" does not pass, so we take the following form: ^ (0 | [1-9] [0-9] *) $
3.3. A 0 or a number that does not start with 0. We can also allow a negative sign at the beginning: ^ (0 | -? [1-9] [0-9] *) $
4.4. This represents a 0 or a number that may be negative and begins with a non-zero. Let the user start with 0. Get rid of the negative sign, too, because the money can't be negative. What we want to add next is the possible decimal part: ^ [0-9] + (. [0-9] +)? $
5.5. It must be noted that there should be at least 1 digit after the decimal point, so "10." No, but "10" and "10.2" are passed: ^ [0-9] + (. [0-9] {2})? $
6.6. In this way, we stipulate that there must be two digits after the decimal point. If you think it is too harsh, you can go like this: ^ [0-9] + (. [0-9] {1mem2})? $
7.7. This allows the user to write only one decimal place. Now it's time for us to consider the comma in the number. We can go like this: ^ [0-9] {1 # 3} (, [0-9] {3}) * (. [0-9] {1 # 2})? $
8.8.1 to 3 digits, followed by any comma + 3 digits, comma becomes optional instead of having to: ^ ([0-9] + | [0-9] {1 ~ 3} (, [0-9] {3}) *) (. [0-9] {1 ~ 2})? $
9. Note: this is the end result. Don't forget that "+" can be replaced with "*" if you think an empty string is acceptable (strange, why?) Finally, don't forget to remove the backslash when using the function. The usual mistakes are here.
This is the end of the answer to the question about what is RegExp. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
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.