正则表达式-提取方括号标记之间的字符串

时间:2019-05-06 06:15:17

标签: javascript php regex

我在[note]some text[/note]这样的字符串中添加了标签,我想在标签之间提取内部文本。

示例文字:

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?

从给定的文本中提取以下内容:

This is the text I want to extract

Another text I want to [text:sample] extract

2 个答案:

答案 0 :(得分:1)

我们可以尝试使用以下正则表达式模式进行匹配:

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

这表示只捕获[note]和最接近的结束[/note]标记之间的内容。请注意,如果有必要,我们会使用[\s\S]*在换行符之间匹配所需的内容。

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);

答案 1 :(得分:1)

我是在Tim张贴答案时写的,这很像,但是我认为我还是会张贴它,因为它被提取为可用于任何标签的可重用函数。

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?']

演示:https://codesandbox.io/s/kw006560oo