Home > OS >  How to split a string into an array before a certain substring not after?
How to split a string into an array before a certain substring not after?

Time:02-01

I have a long string that contains many instances of the substring "trackButtonEvent(", I would like to split the array before the instance of this substring and not afterwards.

Example:

const contents: string = "Here is a file that has multiple instances of the trackButtonEvent(...); and here is another one trackButtonEvent(...); and now here is another occurrence trackButtonEvent(...); this is the end of the file.";

const contentArray: string[] = contents.split("trackButtonEvent(");

console.log("contentArray --> ", contentArray)

// Desired Results
['Here is a file that has multiple instances of the ', 'trackButtonEvent(...); and here is another one ', 'trackButtonEvent(...); and now here is another occurrence ', 'trackButtonEvent(...); this is the end of the file.']

// Actual Results
['Here is a file that has multiple instances of the ', '...); and here is another one ', '...); and now here is another occurrence ', '...); this is the end of the file.']

If I can split the string into an array before the occurrence of a particular word/string instead of afterwards, then I can loop through the array and make a new array of every occurrence of "trackButtonEvent(...);". This would be done by looping through the array and making a new array of the substring of between start= "trackButtonEvent(" and end ");"

function grabAnalyticStats(contents, nameOfAnalytic, start, end) {
  const exists = contents.includes(nameOfAnalytic);
  let itemString = '';
  if (exists) {
    const item = getStringInBetween(contents, start, end);
    itemString = start   item   end;
  }
  return itemString;
}

Ultimately, my goal is to have an array that looks like this: (

['trackButtonEvent(...some code #1);', 'trackButtonEvent(...some code #2);', 'trackButtonEvent(...some code #3);'];

If there is an alternative way to solve for the desired results without splitting before a word then looping through array to find substring between "trackButtonEvent(" and ");" then please let me know! Thank you!

CodePudding user response:

You can use String#match to get all occurrences of a specific pattern.

const contents = "Here is a file that has multiple instances of the trackButtonEvent(...); and here is another one trackButtonEvent(...); and now here is another occurrence trackButtonEvent(...); this is the end of the file.";
let res = contents.match(/trackButtonEvent\(.*?\);/g);
console.log(res);

CodePudding user response:

You can use a positive lookahead to do the split:

const input = "Here is a file that has multiple instances of the trackButtonEvent(...); and here is another one trackButtonEvent(...); and now here is another occurrence trackButtonEvent(...); this is the end of the file.";
const regex = /(?=trackButtonEvent\()/;
const result = input.split(regex);
console.log(result);

Output:

[
  "Here is a file that has multiple instances of the ",
  "trackButtonEvent(...); and here is another one ",
  "trackButtonEvent(...); and now here is another occurrence ",
  "trackButtonEvent(...); this is the end of the file."
]

UPDATE 1 based on additional filtering requirement:

const input = "Here is a file that has multiple instances of the trackButtonEvent(...); and here is another one trackButtonEvent( withStuff(...); ); and now here is another occurrence trackButtonEvent(...); this is the end of the file.";
const regex = /(?=trackButtonEvent\()/;
const result = input
  .split(regex)
  .filter(str => regex.test(str))
  .map(str => str.replace(/^([\s\S]*\);).*/, '$1'));
console.log(result);

Output:

[
  "trackButtonEvent(...);",
  "trackButtonEvent( withStuff(...); );",
  "trackButtonEvent(...);"
]

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

  • Related