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

我已经在字符串中添加了标签,就像[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


慕尼黑的夜晚无繁华
浏览 173回答 2
2回答

胡说叔叔

我们可以尝试使用以下正则表达式模式进行匹配:\[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);
打开App,查看更多内容
随时随地看视频慕课网APP