Home > OS >  Extracting property from another javascript file using jquery
Extracting property from another javascript file using jquery

Time:11-30

My config file is being generated as the following

angular.module('config', [])

.constant('AURL', "http://localhost:4001/AURL")

.constant('BURL', "http://localhost:8000/BURL/v1")

.constant('APP_ENV', "development")
;

I am doing the following to extract data

var configFile = $.getScript("../config.js", function(data) {
  console.log("IT is", data);
});

If I want to get value of BURL how I can do it?

Thank you for helping

CodePudding user response:

Regex solves the problem!

Since getScript delivers a string, I'll just imagine that you already received the data. Check out this code:

// In getScript() callback
const data = `angular.module('config', [])
  .constant('AURL', "http://localhost:4001/AURL")
  .constant('BURL', "http://localhost:8000/BURL/v1")
  .constant('APP_ENV', "development");`;

let burl = data.match(/(?<='BURL', "). (?="\))/)[0]; // Regex solves the problem!! We have a lookbehind with the necessary data (including "BURL"), and some stuff to terminate
console.log(burl);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related