Re is a class in JS that deals specifically with strings; Rules that can be understood as strings; The re can only be used to process strings

There are two ways to write a re: literal constructors

Part of a re: metacharacter modifier

Metacharacters are classified as follows: Special metacharacters quantifier metacharacters Common metacharacters

The lastIndex attribute of the re defines the position at which the next capture will begin

A commonly used metacharacter with special meaning

\d stands for the digits between 0 and 9 \w stands for the letters underscore ^ what does it start with? $What does it end with? [ab] stands for a or B. [a-z] stands for a through Z, that is, lowercase letters [^ab] stands for except a and B [^ a-z] represents in addition to lowercase letters a | b represents a or b () on behalf of the group, is often used to capture, in matching can improve priority (? :) only matches but does not capture (? =) matches only but does not capture forward lookup (? !). Matches only and does not capture negative pre-checksCopy the code

Quantifier metacharacters are used after the corresponding metacharacters

? * indicates that the corresponding character appears 0 or 1 times. + Indicates that the corresponding character appears 1 or more times. {n} Indicates that the corresponding character appears n times. {n,m} indicates that the corresponding character appears n to m timesCopy the code

The modifier is used after two slashes

I ignore case m multi-line match g global matchCopy the code

Validate significant digit

Integer negative decimal /^[-+]? (([1-9]\d*)|0)(\.\d+)? $/Copy the code

Verify mobile phone number

The number 1 at the beginning The second is 3 5 6 7 | | | | | 8 and 9, a total of 11 / ^ 1 [356789] \ d {9} $/Copy the code

Verify the regular of QQ mailbox

\d{4,10} @qq.com \ \.com$/Copy the code

Verify ID card Number

A total of 18 The last may be X / ^ 1-9] [\ d {5} \ d {4} \ d {2} {2} \ d \ d {2} \ d $/ (\ d | X)Copy the code

Verify password

The 6-16 digits are alphanumeric underscores /^\w{6,16}$/Copy the code

The method of encapsulating an execAll of your own can capture all the content that meets the criteria at once

    RegExp.prototype.execAll = function (str=' ') {// First check if the corresponding re has the g modifierif(! This.global){// throw new Error('Your re cannot use this method without the g modifier')
            return; } // how to ensure that STR is a string? str = str.toString(); // this ---> reg2 var res = this.exec(str); var ary = [];while(res){ ary.push(res); res = this.exec(str); / / each timewhileThe loop updates the RES; }return ary
    }
Copy the code

The method that encapsulates a getCookie uses execAll getCookie on a string prototype

   String.prototype.getCookie = function(key){ var reg = /([^; ] +) = (/ ^; +)/g; var ary = reg.execAll(this); // Convert each item in ary to the object format var obj = {}; Ary. ForEach (item=>{// each item in the array of items; Obj (item[1]) = item[2]})return key ? obj[key] : obj
   }
Copy the code

Cupidity and laziness

  • Laziness no longer captures backward after finding a string that matches the rule once

  • How to solve the lazy line? The modifier g

  • Cupidity took all the eligible ones at once

  • How to deal with greed? The corresponding metacharacter ends with?

  • Laziness means taking only once; greed means getting as much as possible at a time.

match

A method that returns array match as a string when the re does not have a global modifier; The effect is the same as exec, but with the global modifier G, match will only capture the contents of all the major re matches. Match (with G) has the advantage of capturing all the contents of the major re, but it cannot capture small groups

replace

  • The two uses of replace are that the second argument can be either a string or a callback function

  • If it is a string, it means that the string is used to replace the content of the current re

  • If it is a function, it means that the return value of the callback function is used to replace what the current re matches. The second argument can be a string or a callback function

  • If it is a string, it means that the string is used to replace the content of the current re

  • Function to replace the current re with the return value of the callback function

The advantage of using replace to implement getParam over Match is that small groups can be captured even with G

    function getParam(str){
        var obj = {};
        str.replace(/([^?=&]+)=([^?=&]+)/g,function(a, b, c) {obj} [b] = c)/writing/arrow function STR. Replace (/ ([^? = &] +) = ([^? = &] +)/g, (a, b, c) = > obj [b] = c)return obj;
    }
Copy the code

Micrometer operator

The first way

 var str = '1234345345';
    var s = ' '
    for(var i = 0 ; i < str.length; i++){
        if(i%3==2){
            s += str[i]+', '
        }else{
            s += str[i]
        }
    }
Copy the code

The second way

 var str = '1234345345';
    str = str.split(' ').reverse().join(' '); Var res = str.replace(/\d{3}/g,function(a){
        return a+', '
    })
    res = res.split(' ').reverse().join(' ');
Copy the code

The third way

var str = '1234345345'; Var res = STR. Replace (/ \ d {1, 3} (? =(\d{3})+$)/g,function(... arg){return arg[0]+', '
   })
Copy the code