skip to Main Content

I’m looking for a pattern that match with

    "changes": [
      "5.12.0",
      "5.14.0"
    ],
...
    "changes": [
      "1",
      "5.0.0",
      "5.10.1"
    ],
...
    "changes": [
      "4.4",
      "5.0.0",
      "5.10.1"
    ],

I’m no expert, I’ve tried maybe 40 or 50 different solutions, here’s my last try:

/"changes": [s*("([0-9](.+))*"(,+))*s*],/

i try this one and it works, ty.

"changes": [s*("([0-9](.+))*"(,+)s )+("([0-9](.+))*"s)],

2

Answers


  1. Chosen as BEST ANSWER

    sorry if i dont clarify, i have a json with 3 000k lines, with a big array of object and i want to delete all changes versions of any object.

    the solution is :

    "changes": [s*("([0-9](.+))*"(,+)s*)+("([0-9](.+))*"s*)],
    

    thank you ! i spend 4 hours this morning and i found some minutes after asking :(


  2. I would do it in two steps:

    1. Search for the list of versions between the brackets after "changes":

      /"changes":s*[s*([^]]+)s*]/gs : https://regex101.com/r/XHaxJ0/1

    2. For each match, you’ll get the list of versions in the capturing group 1:

      "4.4",
            "5.0.0",
            "5.10.1"
      

      You can then extract each version with /[d.]+/g : https://regex101.com/r/XHaxJ0/2

    The Javascript code:

    const input = `    "changes": [
          "5.12.0",
          "5.14.0"
        ],
    ...
        "changes": [
          "1",
          "5.0.0",
          "5.10.1"
        ],
    ...
        "changes": [
          "4.4",
          "5.0.0",
          "5.10.1"
        ],`;
    
    const regexChangesContent = /"changes":s*[s*([^]]+)s*]/gs;
    const regexVersion = /[d.]+/g;
    
    // To fill with the found versions in the changes arrays.
    let versions = [];
    
    let matchChangesContent,
        matchVersion;
    
    while ((matchChangesContent = regexChangesContent.exec(input)) !== null) {
      while ((matchVersion = regexVersion.exec(matchChangesContent[1])) != null) {
        versions.push(matchVersion[0]);
      }
    }
    
    console.log(versions);

    EDIT since question has changed

    As you just want to delete the "changes" entries, I would do the
    following:

    const input = `    "changes": [
          "5.12.0",
          "5.14.0"
        ],
        "date": "01.01.2023",
        "changes": [
          "1",
          "5.0.0",
          "5.10.1"
        ],
        "property": "value",
        "should_stay": true,
        "changes": [
          "4.4",
          "5.0.0",
          "5.10.1"
        ],`;
    
    const regexChanges = /"changes":s*[s*[^]]+s*]s*,/gs;
    
    console.log(input.replace(regexChanges, ''));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search