Determine the type of data using a regular method

Object.prototype.getType=function() {letstr=Object.toString.call(this); //'object XXX'Reg =/ (\w+)/ // match what you want to extract with a spacereturnReg.exec (STR)[1].tolowerCase ()Copy the code

Create a method for execAll that captures everything you want at once

RegExp.prototype.execAll=function() {let_this=this // Since this cannot be assigned directly, create a new variable to assignif(! this.glounb){ //execIf there is no global'g'Only the first item that matches the condition is captured by default, not _this=eval(_this+'g')}let ary=[]
    let res=_this.exec(str)
    while(res){    //whileThe loop stops the loop ary.push(res) when res is null; res=_this.exec(str) }return ary
}
Copy the code

Create a getParm method that splits the URL

function getParm(key){
    let obj={}
    let reg = /([^?=&]+)=([^?=&]+)/;
    str.replace(reg,function(a,b,c){obj[b]=c// a is the content of the entire grand re,b is the content of the first parenthesis,c is the content of the second parenthesis})returnobj[key] ? Obj [key] : obj[key] : obj // check whether the key exists, if yes, return obj key attribute value, if not, return obj whole object}Copy the code

A method of changing uppercase letters in a string to lowercase letters and lowercase letters to uppercase letters

let str='sajhKJAFKAkajhhfakjh'
let reg=/([a-z]*)([A-Z]*)/g
str.replace(reg,function(a,b,c){
   return str+=b.touppercase()+c.tolowercase()
})
Copy the code

Get the most frequent occurrences of a character in a string and the number of occurrences.

The first way

let str='skjakajhfkah'
function getMax(str){
    let obj={}
    for(leti=0; i<str.length; i++){let a =str[i];
        if(obj.hasownproperty('a'(obj[a]+=1}){obj[a]+=1}elseObj [a]=1}} {obj[a]=1}}let key=' ',
       num=0
    for(k in obj){
        if(obj[k]>num){// loop to determine which attribute has the largest value k=key; obj[k]=num } }return{key,// the most common character num// the number of occurrences}} getMax(STR)Copy the code

The second method (using re)

let str='skjakajhfkah'
function getMax2(str){
    let key=' '
    let num=0
    let str=str.split(' ').sort().join(' 'Replace (/(\w)\1*/g,function(a,b){// A represents the entire grand re, and b represents which letter was capturedif(a.length>num){
           num=a.length;
           key=b
        }
    }
    return{ key; }} getMax2(STR)Copy the code

Template engines

let template = 'I am {{name}}, age {{age}}, gender {{sex}}';
    let data = {
        name: 'Ming',
        age: 18
    }
  
    
function render(template, data){
   let str = template.replace(/\{\{\(w+)\}\}/),funciton(a,b){
       return data[b]
    }
    returnstr } render(template, data); I am Xiao Ming, age 18, gender undefinedCopy the code

Micrometer operator

var str='1213131214'
  function rem(str){
    returnSTR. Replace (/ ((\ d {1, 3})? =(\d{3})+$)/g,'$1'  )
  }
  rem(str)
Copy the code