简体   繁体   English

使用正则表达式在后缀和前瞻之间获取带有括号和括号的字符串

[英]Using Regex to get string between lookbehind and lookahead with brackets and parenthesis in it

I've been researching using Regex in my JS project for a few hours now. 我已经在我的JS项目中研究使用Regex了几个小时。 I've run into an issue of trying to get the string between 'tags'. 我遇到了尝试在“标签”之间获取字符串的问题。 The only issue I have now is the characters I am using in the tag. 我现在唯一的问题是标签中使用的字符。

String I'm looking at: 我正在查看的字符串:

[](/TABLE_1)
##Table 1
Header 1|Header 2|Header 3
:-:|:-:|:-:
Value 1|Value 2|Value 3
Value 1|Value 2|Value 3
[](/END_TABLE_1)

[](/TABLE_2)
##Table 2
Header 1|Header 2|Header 3
:-:|:-:|:-:
Value 1|Value 2|Value 3
Value 1|Value 2|Value 3
[](/END_TABLE_2)

[](/TABLE_3)
##Table 3
Header 1|Header 2|Header 3
:-:|:-:|:-:
Value 1|Value 2|Value 3
Value 1|Value 2|Value 3
[](/END_TABLE_3)

I am attempting to get the string inside 我正在尝试将字符串放入里面

[](/TABLE_1)

and

[](/TABLE_2)

Currently, I am attempting to use the Regex expression: 当前,我正在尝试使用Regex表达式:

(?<=[](/TABLE_1))(.*)(?=[](/END_TABLE_1))

I noticed that the tags I have, which I can't change, are not able to be detected in that. 我注意到我无法更改的标签无法在其中被检测到。

You have to escaped ()[] because they are recognized as metacharacters in regex and you shouldn't use (?<=)(?=) positive lookbehinds in JavaScript. 您必须转义()[]因为它们在正则表达式中被识别为元字符,并且您不应该在JavaScript中使用(?<=)(?=)正向隐藏。

Regex : \\[\\]\\(\\/(TABLE_[12])\\)\\n([\\S\\s]+)\\n\\[\\]\\(\\/END_\\1\\) 正则表达式\\[\\]\\(\\/(TABLE_[12])\\)\\n([\\S\\s]+)\\n\\[\\]\\(\\/END_\\1\\)

 var text = `[](/TABLE_1) ##Table 1 Header 1|Header 2|Header 3 :-:|:-:|:-: Value 1|Value 2|Value 3 Value 1|Value 2|Value 3 [](/END_TABLE_1) [](/TABLE_2) ##Table 2 Header 1|Header 2|Header 3 :-:|:-:|:-: Value 1|Value 2|Value 3 Value 1|Value 2|Value 3 [](/END_TABLE_2) [](/TABLE_3) ##Table 3 Header 1|Header 2|Header 3 :-:|:-:|:-: Value 1|Value 2|Value 3 Value 1|Value 2|Value 3 [](/END_TABLE_3)`; var re = /\\[\\]\\(\\/(TABLE_[12])\\)\\n([\\S\\s]+)\\n\\[\\]\\(\\/END_\\1\\)/g; var m; do { m = re.exec(text); if (m) { console.log(m[2]); } } while (m); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM