Front end regular exercises

Time:2021-1-30
Exercises
1. Change get element by ID to hump name
     var str = 'get-element-by-id'
     const reg = /-\w/g
     const newStr = str.replace(reg, word => {
           console.log(word, 'word12')
           return word.substring(1).toUpperCase()
        })
    console.log(newStr, 'newStr')
    // getElementById
2. Judge the telephone number
     function isPhone(tel) {
        var regx = /^1[34578]\d{9}$/;
        return regx.test(tel);
       }
3. Find repeated words
var str = "Is is the cost of of gasoline going up up";
var patt1 = /\b([a-z]+) \b/ig;
document.write(str.match(patt1));

 specifies the first child back reference~~~~
4. Abcdebbcde matching reduplication
https://www.cnblogs.com/guorange/p/6693168.html
Source string: abcdebbcde

Regular expression: ([AB]) 1

For the regular expression "([AB]) 1", the sub expression "[AB]" in the capture group can match
Match "a" or "B", but once the capture group is matched successfully, the content of the reverse reference is determined. as
If the capture group matches "a", the reverse reference can only match "a". Similarly, if the capture group matches "a", the reverse reference can only match "a"
If "B" is assigned, the reverse reference can only match "B". Due to the limitation of backward reference "1"
System, requires two identical characters, here is "AA" or "BB" to match
It's a lot of work. ~~~~
5. Given string STR, check whether it conforms to the following format

XXX-XXX-XXX where x is the number type

  function matchesPattern(str) {
    
  return /^(\d{3}-){2}\d{4}$/.test(str);
6. Implementation of thousands separator in JS

The front end does not look back, so it only talks about the future
First of all, we need to understand the parsing order of regularization. The parsing order of regularization is from left to right

    1. ?=Forward looking, for example, / b (? = a) /. Test (‘bab ‘) to see if the forward looking part is a / / true
    1. ?!Negative forward looking / b (?! a) /. Test (‘bb ‘) to see if the forward looking part is not a / / true
    const number = 123456789123
            var regx = /\d{1,3}(?=(\d{3})+$)/g
            // document.write((number + '').replace(regx, '$&,'))
            //$& represents the last 123 of the string 123 456 789 that matches regx 
            //It is not matched because? = there is no 3-digit ~ ~ ~ word when looking forward
            document.write(
              (number + '').replace(regx, function (world) {
                //The world here is the $& 123 456 789
                return world + ','
              }),
            ) ~~~~