user3596335
user3596335

Reputation:

Get groups between two strings

So I got a string:

let string = "XABXAX12345BX293993AX9393B33AXAXBXBXBXAAABBX";

and I'd like to extract all occurrences between the strings AX and BXs to get an array like this as result:

let result = ["12345", "9393B33AXAX"];

I've tried to use some kind of regex but I was not really successfull tbh.

let result = string.split(/AX([^AXBX]+)BX/);

Another aproach was a simple for-loop but also this is not working as I've expected. So maybe somebody is able to help me fixing the issues. Please have a look at my code:

let string = "XABXAX12345BX293993AX9393B33AXAXBXBXBXAAABBX"

let result = [];
for (let i=0; i<string.length; i++) {
  if (string[i] == "A" && string[i+1] === "X") {
    for (let j=i; j<string.length; j++) {
      if (string[j] == "B" && string[j+1] === "X") {
        let substring = string.substring(i+1, j+1);
        result.push(substring)
        break;
      }
    }
  }
}

console.log(result);

Upvotes: 1

Views: 56

Answers (2)

Kevin Ji
Kevin Ji

Reputation: 10499

Here's a relatively straightforward looping approach that doesn't use regexes:

function findOccurrences(str, fromStr, toStr) {
  const occurrences = [];
  let startIndex = 0;
  
  while (true) {
    const fromIndex = str.indexOf(fromStr, startIndex);
    if (fromIndex === -1) {
      break;
    }

    const toIndex = str.indexOf(toStr, fromIndex + fromStr.length);
    if (toIndex === -1) {
      break;
    }

    const occurrence = str.slice(fromIndex + fromStr.length, toIndex);
    occurrences.push(occurrence);
    startIndex = toIndex + toStr.length;
  }

  return occurrences;
}

console.log(
  findOccurrences("XABXAX12345BX293993AX9393B33AXAXBXBXBXAAABBX",
    "AX", "BX"));

This doesn't include any sanity checks; for instance, you might want to check that fromStr and toStr aren't empty strings.

Upvotes: 1

melpomene
melpomene

Reputation: 85857

Here's a simple solution:

function re_esc(str) {
    return str.replace(/\W/g, "\\$&");
}

const start = "AX";
const end = "BX";

const re = new RegExp(re_esc(start) + '([\\s\\S]*?)' + re_esc(end), 'g');

const string = "XABXAX12345BX293993AX9393B33AXAXBXBXBXAAABBX";
const results = [];

let m;
while (m = re.exec(string)) {
    results.push(m[1]);
}

console.log(results);

We build a regex of the form START(.*?)END, then use it to successively extract matches in a loop.

Upvotes: 2

Related Questions