我的 changelog.md 資料如下所示。
## 11.2.3
* xxx
* xxxx
## 11.2.2
* ttt
* ttt
我需要一個只能回傳第一個版本串列的正則運算式,即。
* xxx
* xxxx
我嘗試了多種解決方案,但沒有達到最終結果。
我試過匹配,替換它,沒有用。
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);
uj5u.com熱心網友回復:
你不需要放在^##里面[]。方括號是用來制作字符集的,()是用來分組的。如果您對該部分不感興趣,則無需對其進行分組。
使用m修飾符^匹配行的開頭而不是字串的開頭。然后.*\n將匹配該行的其余部分。[^#]*將匹配之后的所有內容,直到下一個#。
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]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485690.html
標籤:javascript 正则表达式
下一篇:正則運算式匹配中的Perl回圈
