有人可以帮助我提取文本与RegExp

Somebody can helpme to extract a text with RegExp?

本文关键字:提取 取文本 RegExp 帮助      更新时间:2023-09-26

今天我被一个正则表达式弄得头破脑裂。我无法摘录文本的一部分。我的文本是这样的:

<!--TEXT[title]-->
sometext 1
<!--END-->
<!--TEXT[title]-->
sometext 2
<!--END-->

我想把它放到数组中

["title]-->sometext1"
,"title]-->sometext2"]

我有这个正则表达式代码mytext.match(/<!--TEXT[([.|'w|'r|'n]+)<!--END-->/m);

假设您需要一个正则表达式,下面应该可以工作:

<'!--TEXT'[([^']]*)']-->'s*'n(.*)(?!<'!--END-->)

如果这个文本是在DOM中,那么解析DOM会更好。

解释:

<'!--TEXT'[ // Match the start.
([^']]*) // Match (in group 1), everything up until the next ']'
']-->'s*'n // Match to the end of this line.
(.*) // Match anything (in group 2).
(?!<'!--END-->) // Stop before the end tag is next. (This will mean you get everything up to, but not including the previous line break).