-2

I've tags in string like [note]some text[/note] where I want to extract the inside text between the tags.

Sample Text:

Want to extract data [note]This is the text I want to extract[/note] 
but this is not only tag [note]Another text I want to [text:sample] 
extract[/note]. Can you do it?

From the given text, extract following:

This is the text I want to extract

Another text I want to [text:sample] extract

Sovon
  • 1,804
  • 1
  • 22
  • 30

2 Answers2

1

We can try matching using the following regex pattern:

\[note\]([\s\S]*?)\[\/note\]

This says to just capture whatever comes in between [note] and the closest closing [/note] tag. Note that we use [\s\S]* to potentially match desired content across newlines, should that be necessary.

var re = /\[note\]([\s\S]*?)\[\/note\]/g;
var s = 'Want to extract data [note]This is the text I want to extract[/note]\nbut this is not only tag [note]Another text I want to [text:sample]\n extract[/note]. Can you do it?';
var m;

do {
    m = re.exec(s);
    if (m) {
        console.log(m[1]);
    }
} while (m);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

I wrote this while Tim posted his answer and it's quite like but I figured I'll post it anyway since it's extracted to a re-usable function you can use for any tag.

const str = `Want to extract data [note]This is the text I want to extract[/note] 
but this is not only tag [note]Another text I want to [text:sample] 
extract[/note]. Can you do it?`;

function extractTagContent(tag, str) {
  const re = new RegExp(`\\[${tag}\\](.*?)\\[\\/${tag}\\]`, "igs");
  const matches = [];
  let found;
  while ((found = re.exec(str)) !== null) {
    matches.push(found[1]);
  }
  return matches;
}

const content = extractTagContent("note", str);
// content now has:
// ['This is the text I want to extract', 'Another text I want to [text:sample] extract. Can you do it?']

Demo: https://codesandbox.io/s/kw006560oo

Oskar Hane
  • 1,824
  • 13
  • 8