Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex (JS) read data between two symbols

My changelog.md data looks like below.

## 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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. Then .*\n will match the rest of that line. [^#]* 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]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading