简体   繁体   English

正则表达式(JS)读取两个符号之间的数据

[英]Regex (JS) read data between two symbols

My changelog.md data looks like below.我的 changelog.md 数据如下所示。

## 11.2.3
* xxx
* xxxx

## 11.2.2
* ttt
* ttt

I need a regex that can return only the first versions list ie.我需要一个只能返回第一个版本列表的正则表达式,即。

* xxx
* xxxx

I tried multiple solutions but, didn't reach the final result.我尝试了多种解决方案,但没有达到最终结果。

I tried matching, and replacing it, didn't work.我试过匹配,替换它,没有用。

 const changelog = ` ## Hello World * This is a bold text * This is a bold text ## Hello World * This is a bold text ## Hello World * This is a bold text `; const final = changelog.match( /([^(##)])(.*)[^(##)]/g ); console.log(final);

You don't need to put ^## inside [] .您无需将^##放入[]中。 Square brackets are for making character sets, () is for grouping.方括号用于制作字符集, ()用于分组。 There's no need to group it if you're not interested in that part.如果您对该部分不感兴趣,则无需对其进行分组。

Use the m modifier to make ^ match the beginning of a line instead of the the beginning of the string.使用m修饰符使^匹配一行的开头而不是字符串的开头。 Then .*\n will match the rest of that line.然后.*\n将匹配该行的其余部分。 [^#]* will match everything after that until the next # . [^#]*将匹配之后的所有内容,直到下一个#

 const changelog = ` ## Hello World * This is a bold text * This is a bold text ## Hello World * This is a bold text ## Hello World * This is a bold text `; const final = changelog.match(/^##.*\n([^#]*)/m); console.log(final[1]);

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

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