JavaScript对象删除特定键值中的引号

时间:2015-12-15 21:11:17

标签: javascript regex

我需要删除特定键值的双引号:

upload: {
    name: "upload",
    url: "/upload",
    controller: "invoicesCtrl",
    templateUrl: "./views/invoices.html",
    resolve: "resolveUrl('./compiled/invoices_app.js')"
  },
  invoices: {
    name: "invoices",
    url: "/invoices",
    controller: "invoicesCtrl",
    templateUrl: "/invoices/views/invoices.html",
    resolve: "resolveUrl('/invoices/app/js/compiled/invoices_app.js')"
  }

只是为了解析键值,其他键必须是字符串。使用replace函数是否有任何正则表达式?预期结果:

upload: {
    name: "upload",
    url: "/upload",
    controller: "invoicesCtrl",
    templateUrl: "./views/invoices.html",
    resolve: resolveUrl('./compiled/invoices_app.js')
  },
  invoices: {
    name: "invoices",
    url: "/invoices",
    controller: "invoicesCtrl",
    templateUrl: "/invoices/views/invoices.html",
    resolve: resolveUrl('/invoices/app/js/compiled/invoices_app.js')
  }

1 个答案:

答案 0 :(得分:3)

当您说删除双引号并且resolve中唯一的双重配件是字符串文字终结符时,您是否说要使用名为{{1}的JavaScript函数在某个时候被调用,而resolveUrl应该是该调用的结果?

在这种情况下,您可以执行以下操作:

resolve

如果您知道它始终是// iterate through all properties of myObject, assuming it is a flat // object containing upload and invoices from your code Object.keys(myObject).forEach(function(key) { if(myObject[key].hasOwnProperty('resolve')) myObject[key].resolve = eval(myObject[key].resolve); }); 来电,那么让原始属性包含字符串值会更安全 - 例如。 resolveUrl - 然后手动调用resolve: '/invoices/app/js/compiled/invoices_app.js'

resolveUrl