Home > OS >  Regex (JS) read data between two symbols
Regex (JS) read data between two symbols

Time:06-03

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.

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

CodePudding user response:

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

  • Related