js regular match ${XXX}

Regular match ${XXX}

Determine whether the content in the string ${} is in the specified array

 buttonArr: ["${
    
    aaa}", "${
    
    bbb}", "${
    
    identifier}"],
 created() {
    
    
    this.getPlaceholder('/.ota/device/inform/${aaa}/${bbb}')
  },

  getPlaceholder(url) {
    
    
      let reg_g = /\$\{
    
    (.*?)\}/g;//(.*?)\}表示匹配任意字符到离它最近的'}'
      let result = url.match(reg_g);
      console.log(result)
      let isFlag = true;
        result.map((item) => {
    
    
          let index = this.buttonArr.indexOf(item);
          if (index < 0) {
    
    

            isFlag = false;

          }
        });
  
       console.log(isFlag)
      return isFlag
    },

Running result:
Insert image description here
If ${} is not the specified character

this.getPlaceholder('/.ota/device/inform/${aaa}/${ccc}')

Insert image description here

おすすめ

転載: blog.csdn.net/L221545/article/details/132582542