JavaScript knowledge points-regular

Regular

1. Concept

Regular: It is a logical formula for string manipulation. In fact, some special representative characters defined in advance are used to form a regular string to perform some search and replace operations on the string.

Second, create a regular

  1. Literal creation method: var reg = /rule string/modifier;
   var reg = /web/ig;
   console.log(reg);///web/gi
  1. Constructor creation method: var reg = new RegExp('rule string','modifier');
    var reg1 = new RegExp('web', 'ig');
    console.log(reg1);///web/gi

Three, modifier

  1. i: ignore case ignore case

  2. g: global global match

   var str = 'web123WEB456web789web000';
   var reg = /web/gi;
   var s = str.replace(reg, 'python');
   console.log(s);//python123python456python789python000

Four, string method

  1. Replace: string.replace(regular, new character); return a new string, the original string will not change
   var str = 'web123WEB456web789web000';
   var reg = /web/gi;
   var s = str.replace(reg, 'python');
   console.log(s);//python123python456python789python000
  1. Separate: string.split (regular); return a new array
   var str = 'web123Web456web789web000';
   var reg = /web/gi;
   var arr = str.split(reg);
   console.log(arr)//(5) ["", "123", "456", "789", "000"]
  1. Selection: string.match (regular); Select the characters that meet the conditions, form a new array and return back
   var str = 'web123Web456web789web000';
   var reg = /\d/ig; // 匹配数字
   var arr2 = str.match(reg);
   console.log(arr2);//(12) ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "0", "0"]
  1. Search: string.search(regular); start matching from the position where the subscript is 0, and return the subscript after matching the regular character
   var str = 'web123Web456web789web000';
   var reg = /\d/ig; // 匹配数字
   console.log(str.search(reg));//3

5. Retrieval method (exec, test)

  1. exec: regular.exec (string to be matched); an array of qualified characters will be returned each time

  2. test: regular.test (the string to be matched); returns whether the match is successful, if the match is successful, it returns true, if it fails, it returns false

*If the modifier g is not added, the search or match starts from 0 each time. If g is added, it means that the match starts from the next position of the previous match. After the match result returns null or false, the next match Rematch from 0

*reg.lastIndex Get the current position where the regular match starts

   var str = 'web123web456';
   var reg = /\d\d\d/g;
   console.log(reg.lastIndex); // 0
   console.log(reg.exec(str)); // ["1", index: 3, input: "web123web456", groups: undefined]
   console.log(reg.lastIndex); // 4
   console.log(reg.exec(str)); // 2
   console.log(reg.lastIndex); // 5
   console.log(reg.exec(str)); // 3
   
   console.log(reg.lastIndex); // 0
   console.log(reg.test(str)); // true
   console.log(reg.lastIndex); // 6
   console.log(reg.test(str)); // true
   console.log(reg.lastIndex); // 12
   console.log(reg.test(str)); // false
   console.log(reg.lastIndex); // 0
   console.log(reg.test(str)); // true

Six, meta characters

  1. Dot.: matches all characters except newline
   var str = '\n 123';
   var reg = /./;
   console.log(reg.exec(str));//[" ", index: 1, input: "↵ 123", groups: undefined]
  1. []: Character set, the character set of characters to be matched, no need to add, separate, or quote marks in the character set [^]: Character set, the character set of characters to be matched
   var str = 'web123web456';
   var reg = /[413]/ig;
   console.log(reg.exec(str)); // 1
   console.log(reg.exec(str)); // 3
  1. \d: match digits in the character set 0-9==\d \D: match non-digits
   var str = 'web123';
   var reg = /\d\d\d/ig;
   console.log(reg.exec(str)); // 123
   var reg = /\D/ig;
   console.log(reg.exec(str)); // w
   
   // 银行卡密码 6位数字
   var reg = /\d\d\d\d\d\d/;
   var str = 'a12345';
   console.log(reg.test(str));//false
  1. \s: match spaces \S: match non-spaces
   var str = 'web 123';
   var reg = /\s/ig;
   console.log(reg.exec(str)); // 3
   var reg = /\S/ig;
   console.log(reg.exec(str)); // 0
  1. \w: match numbers, letters, _ \W: match non-digits, letters, _
   var str = '_w1.23*';
   var reg = /\w/ig;
   console.log(reg.exec(str)); // _
   var reg = /\W/ig;
   console.log(reg.exec(str)); // .
  1. \b: match word boundary \B: match non-word boundary
   var str = 'you are a beautiful boy';
   var reg = /\ba/ig; // 匹配a左边是边界的a
   console.log(reg.exec(str)); // 4
   var reg = /a\B/ig; // 匹配a右边不是单词边界
   console.log(reg.exec(str)); // 4
  1. ^: what starts with $: ends with
   var str = '912345';
   var reg = /^\d\d\d\d\d\d$/;
   console.log(reg.test(str)); //true
   console.log(reg.exec(str)); //["912345", index: 0, input: "912345", groups: undefined]

Seven, multiple characters

  1. a?: means one or zero
   var str = '1web123';
   var reg = /\d?/ig;
   console.log(reg.exec(str));//["1", index: 0, input: "1web123", groups: undefined]
  1. a*: Means to match 0 or multiple consecutive characters, to match as many as possible
   var str = '123456web33333';
   var reg = /\d*/;
   console.log(reg.exec(str));//["123456", index: 0, input: "123456web33333", groups: undefined]
  1. a+: Means to match at least one or more consecutive characters, to match as many as possible
   var str = 'webweb3444';
   var reg = /\d+/;
   console.log(reg.exec(str));//["3444", index: 6, input: "webweb3444", groups: undefined]
  1. a{n,m}: means to match at least n times and at most m matches
  • a{n}: means only match n times
   var str = 'web1234567890';
   var reg = /\d{3}/;
   console.log(reg.exec(str)); // 123
  • a{n,}: match at least n times
   var str = 'web1234567890';
   var reg = /\d{3,}/;
   console.log(reg.exec(str)); // 1234567890
  • a{n,m}: means to match at least n times and at most m matches
   var str = 'web1234567890';
   var reg = /^web\d{3,6}/;
   console.log(reg.exec(str)); // web123456

8. Other characters

  1. |: or

  2. (): Group to obtain the matching value of the group: RegExp.$1 RegExp.$2

   var str = 'web1zweb2web3';
   var reg = /web(1|5)(w|z)/;
   console.log(reg.exec(str)); //  ["web1z", "1", "z", index: 0, input: "web1zweb2web3", groups: undefined]
   console.log(RegExp.$1);//1
   console.log(RegExp.$2);//z
  1. (?:): Non-acquisition match
   var str = 'web1zweb2web3';
   var reg = /web(?:5|2)/; // web后面是5或者是2的web
   console.log(reg.exec(str));//["web2", index: 5, input: "web1zweb2web3", groups: undefined]
  1. (?=): Positive affirmative pre-check
   var str = 'web123webdffff';
   var reg = /web(?=\d+)/; // web后面必须跟数字
   console.log(reg.exec(str));//["web", index: 0, input: "web123webdffff", groups: undefined]
  1. (?!): Positive negative pre-check
   var reg = /web(?!\d+)/; // web后面不跟数字的
   console.log(reg.exec(str));//["web", index: 6, input: "web123webdffff", groups: undefined]
  1. (?<=): Reverse affirmative pre-check
   var reg = /(?<=\d+)web/; // web的前面是跟数字的web
   console.log(reg.exec(str)); // 6
  1. (?<!): Reverse negative pre-check
   var reg = /(?<!\d+)web/;
   console.log(reg.exec(str)); // 0
   
   // 不能全是数字的组合
   var str = '124456';
   var reg = /(?!^\d+$)^[0-9a-zA-Z]+$/;
   console.log(reg.test(str)); // false
   
   // 必须是数字和字母的组合 不能全是数字、不能全是字母
   var reg = /(?!^\d+$)(?!^[a-zA-Z]+$)^[0-9a-zA-Z]+$/;
   var str = '1223a3344';
   console.log(reg.test(str)); // false

Commonly used regular

1. The expression of the check digit

  1. Number: ^[0-9]*$

  2. n-digit number: ^\d{n}$

  3. At least n digits: ^\d{n,}$

  4. mn digits: ^\d{m,n}$

  5. Numbers starting with zero and non-zero: ^(0|[1-9][0-9]*)$

  6. Numbers starting with non-zero and up to two decimal places: ^([1-9][0-9]*)+(.[0-9]{1,2})?$

  7. Positive or negative numbers with 1-2 decimal places: ^(-)?\d+(.\d{1,2})?$

  8. Positive, negative, and decimal: ^(-|+)?\d+(.\d+)?$

  9. Positive real numbers with two decimal places: ^[0-9]+(.[0-9]{2})?$

  10. Positive real numbers with 1~3 decimal places: ^[0-9]+(.[0-9]{1,3})?$

  11. Non-zero positive integer: ^[1-9]\d*$ or ^([1-9][0-9] ){1,3}$ or ^+?[1-9][0-9] $

  12. Non-zero negative integer: ^-[1-9][]0-9" $ or ^-[1-9]\d $

  13. Non-negative integer: ^\d+$ or ^[1-9]\d*|0$

  14. Non-positive integer: ^-[1-9]\d*|0$ or ^((-\d+)|(0+))$

  15. Non-negative floating point number: ^\d+(.\d+)?$ or ^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+| 0$

  16. Non-positive floating point number: ^((-\d+(.\d+)?)|(0+(.0+)?))$ or ^(-([1-9]\d*.\d*|0 .\d*[1-9]\d*))|0?.0+|0$

  17. Positive floating point number: ^[1-9]\d*.\d*|0.\d*[1-9]\d*$ or ^(([0-9]+.[0-9] [1 -9][0-9] )|([0-9] [1-9][0-9] .[0-9]+)|([0-9] [1-9][0-9 ] ))$

  18. Negative floating point numbers: ^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$ or ^(-(([0-9]+.[0 -9] [1-9][0-9] )|([0-9] [1-9][0-9] .[0-9]+)|([0-9] [1-9 ][0-9] )))$

  19. Floating point number: ^(-?\d+)(.\d+)?$ or ^-?([1-9]\d*.\d*|0.\d*[1-9]\d*|0 ?.0+|0)$

Two, the expression of the check character

  1. Chinese characters: ^[\u4e00-\u9fa5]{0,}$

  2. English and numbers: ^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$

  3. All characters with a length of 3-20: ^.{3,20}$

  4. A string consisting of 26 English letters: ^[A-Za-z]+$

  5. A string consisting of 26 uppercase English letters: ^[AZ]+$

  6. A string consisting of 26 lowercase English letters: ^[az]+$

  7. A string consisting of numbers and 26 English letters: ^[A-Za-z0-9]+$

  8. A string consisting of numbers, 26 English letters or underscores: ^\w+$ or ^\w{3,20}$

  9. Chinese, English, numbers including underscores: ^[\u4E00-\u9FA5A-Za-z0-9_]+$

  10. Chinese, English, numbers but not including underscore and other symbols: ^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$

  11. You can enter characters such as ^%&',;=? KaTeX parse error: Can't use function'\"' in math mode at position 1: \̲"̲: [^%&',;=? \x22] + 12 It is forbidden to input characters containing ~: [^~\x22]+

Three, special demand expressions

  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. Mobile phone number: ^(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. Phone number ("XXX-XXXXXXX", "XXXX-XXXXXXXX", "XXX-XXXXXXX", "XXX-XXXXXXXX", "XXXXXXX" and "XXXXXXXX): ^((\d{3,4}-)|\d {3.4}-)?\d{7,8}$

  6. Domestic telephone number (0511-4405222, 021-87888822): \d{3}-\d{8}|\d{4}-\d{7}

  7. ID number (15 digits, 18 digits): ^\d{15}|\d{18}$

  8. Short ID number (number, letter x ending): ^([0-9]){7,18}(x|X)?$ or ^\d{8,18}|[0-9x]{8, 18}|[0-9X]{8,18}?$

  9. Whether the account number is legal (beginning with a letter, allowing 5-16 bytes, allowing alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

  10. Password (beginning with a letter, length between 6~18, can only contain letters, numbers and underscores): ^[a-zA-Z]\w{5,17}$

  11. Strong password (must contain a combination of uppercase and lowercase letters and numbers, cannot use special characters, and the length is between 8-10): ^(?=. \d)(?=. [az])(?=.*[AZ ]).{8,10}$

  12. Date format: ^\d{4}-\d{1,2}-\d{1,2}

  13. 12 months of a year (01~09 and 1~12): ^(0?[1-9]|1[0-2])$

  14. 31 days of a month (01~09 and 1~31): ^((0?[1-9])|((1|2)[0-9])|30|31)$

  15. Money input format:

  16. 1. There are four representations of money that we can accept: "10000.00" and "10,000.00", and "10000" and "10,000" without "points": ^[1-9][0-9]*$

  17. 2. This means any number that does not start with 0, but it also means that a character "0" does not pass, so we use the following form: ^(0|[1-9][0-9]*) $

  18. 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]*)$

  19. 4. This means a 0 or a possibly negative number that does not start with 0. Let the user start with 0. Remove the negative sign as well, because money can't be negative. What we want to add below is an explanation Possible decimal part: ^[0-9]+(.[0-9]+)?$

  20. 5. It must be noted that there should be at least 1 digit after the decimal point, so "10." is not passed, but "10" and "10.2" are passed: ^[0-9]+(.[0- 9]{2})?$

  21. 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 do this: ^[0-9]+(.[0-9]{1,2})?$

  22. 7. This allows the user to write only one decimal place. Now we should consider the comma in the number, we can do this: ^[0-9]{1,3}(,[0-9]{3})*( .[0-9]{1,2})?$

  23. 8.1 to 3 digits, followed by any comma + 3 digits, the comma becomes optional, not mandatory: ^([0-9]+|[0-9]{1,3}(,[0-9 ]{3})*)(.[0-9]{1,2})?$

  24. Remarks: This is the final result, don’t forget that "+" can be replaced with "*". If you think an empty string is also acceptable (strange, why?) Finally, don’t forget to remove the backslash when using the function Kong, the general errors are here

  25. xml 文件 : ^ ([a-zA-Z] ±?) + [a-zA-Z0-9] + \. [x | X] [m | M] [l | L] $

  26. Regular expression of Chinese characters: [\u4e00-\u9fa5]

  27. Double-byte characters: [^\x00-\xff] (including Chinese characters, can be used to calculate the length of the string (a double-byte character counts as 2, ASCII character counts as 1))

  28. Regular expression for blank lines: \n\s*\r (can be used to delete blank lines)

  29. Regular expression of HTML markup: <(\S*?)[^>] >. ?</\1>|<.*? /> (The version circulated on the Internet is too bad, and the above is only part of it. For complex The nested tag is still helpless)

  30. Regular expression of the first and last blank characters: ^\s*|\s* KaTeX parse error: Undefined control sequence: \s at position 4: or (^\̲s̲*)|(\s* ) (can be used to delete the beginning of a line White space at the end of the line (including spaces, tabs, form feeds, etc.), very useful expressions)

  31. Tencent QQ number: [1-9][0-9]{4,} (Tencent QQ number starts from 10000)

  32. Chinese postal code: [1-9]\d{5}(?!\d) (Chinese postal code is 6 digits)

  33. IP address: \d+.\d+.\d+.\d+ (useful when extracting IP address)

  34. IP address: ((?: (?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5 ]|2[0-4]\d|[01]?\d?\d))

Guess you like

Origin blog.csdn.net/qq_41008567/article/details/107109194