js处理java中的占位符,全局替换括号中的数据

  data() {
    return {
        url: 'https://www.baidu.com/addUser/{id}/{userName}/{age}/app',
        urlObj: {id: 9527, userName: 'zhouxingchi', age: 56},
    }
  }
  methods:{
      placeholder() {
         if (this.url.indexOf("{") != -1) {
            var reg = /\{(.+?)\}/g
            var urlArr = this.url.match(reg)
            urlArr.forEach(val => {
              var valTo = val.substring(1, val.length - 1)
              this.url = this.url.replace(val, this.urlObj[valTo])
            })
          } 
        },
      }

本文章由javascript技术分享原创和收集

发表评论 (审核通过后显示评论):