0

I have string that looks like :

"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"'

How can I create a regex that outputs an array like :

{abc.xml, def.xml} or {"test-file" : "abc.xml","test-file" : "def.xml"} 

that is only pairs with test-file before ‍‍':'.

I tried :

json.match(/"test-file" : "(.*)\.xml"/); 

but I am getting output:

0: "\"test-file\" : \"abc.xml\",\"test2-file\":\"abcd.xml\",\"test3-file\":\"bcde.xml\",\"test-file\" : \"def.xml\""
​ 1: "abc.xml\",\"test2-file\":\"abcd.xml\",\"test3-file\":\"bcde.xml\",\"test-file\" : \"def"

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Dhruv
  • 3
  • 3
  • `{abc.xml, def.xml}` That's an invalid object. `or {"test-file" : "abc.xml","test-file" : "def.xml"}` That will result in only one key-value pair. – CertainPerformance Aug 18 '18 at 05:40
  • @CertainPerformance thanks for correction, but is there a possible regex for this kind of problem ? – Dhruv Aug 18 '18 at 05:44
  • @wp78de This json subtree could be part of a bigger json with variable keys, so instead of calling '.values; repeatedly (since json structure is unknown), I though of converting json to string and look up for specific keys value pairs using regex. is it even possible ? – Dhruv Aug 18 '18 at 06:04
  • To get your desired result, make `.*` [reluctant](https://www.rexegg.com/regex-quantifiers.html#lazy): [`"test-file" : "(.*?\.xml)`](https://regex101.com/r/IptZZE/1/) – bobble bubble Aug 18 '18 at 20:50

2 Answers2

0

Store the value in a string and use split function

Example:

test_string='"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"';
test_string.split(",");

It will split that string by , and store values in an array.

Paolo
  • 21,270
  • 6
  • 38
  • 69
0

If all the key-value pairs you are looking for are

  • on the same node or
  • the same level

you should be fine using JSON directly.

I doubt that regex will be helpful when you have to deal with variable key names. There must be a criterion that allows you to distinguish the good keys from the bad keys.

If this criterion is the order, here is a sample built around Object.keys to access the values without knowing the actual name of the key. However, there are many other ways to do this.

// Function to get the nth key from the object
Object.prototype.getByIndex = function(index) {
  return this[Object.keys(this)[index]];
};

var json = {
    "config": {
        "files": [{
            "name1": "test.xml"},
        {
            "name2": "test2.xml"
        }]
    }
};
$.each(json.config.files, function(i, v) {
    if (i !== 0) // or whatever is a "good" index
        return;
    //or if it is the content of the value the identifies a good value...
    if (v.getByIndex(0).indexOf(".xml") !== -1) {
        console.log(v);
        return;
    }    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

If you do a lot of this JSON querying operations a JSON query language/library like JSONiq, JSPath, or jsonpath could be the right thing for you.


If your substring/key is always the same you can indeed make use of regex, e.g.

const regex = /"test-file"\s*:\s*".*?\.xml"/g;
const str = `"test-file" : "abc.xml","test2-file":"abcd.xml","test3-file":"bcde.xml","test-file" : "def.xml"'`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
wp78de
  • 18,207
  • 7
  • 43
  • 71
  • thanks for the response, I think this solution is good but the issue is that Json derived from a schema that can vary during different requests, rather than hard coding it for those schemas that can be quite large in number, I was interested in a subtring that will always be the same that is like “test-file”:”abc.xml”. In one case it can have 1 parent in other case it can have more than one parent so json parsing it is little difficult. – Dhruv Aug 18 '18 at 18:52