[Solved] Getting the words between a pattern in a String in Javascript


Here is a better way to do it:

let text = `Some text here
hello FirstComponent hello-end a bit more text here
hello SecondComponent hello-end a bit more text there
hello ThirdComponent hello-end
some text there`

function extractContent(input, startTag, endTag) {
  const re = new RegExp("("+ startTag + ")(.|\n)+?(" + endTag + ")", 'g');
  const result = [];
  let match;

  while ((match = re.exec(input)) !== null) {
    result.push(match[0]);
  }
  return result;
}

console.log(extractContent(text, "hello", "hello-end"));

0

solved Getting the words between a pattern in a String in Javascript