我正在使用灰質將檔案系統中的 .MD 檔案決議為字串。決議器產生的結果是這樣的字串:
\n# Clean-er ReactJS Code - Conditional Rendering\n\n## TL;DR\n\nMove render conditions into appropriately named variables. Abstract the condition logic into a function. This makes the render function code a lot easier to understand, refactor, reuse, test, and think about.\n\n## Introduction\n\nConditional rendering is when a logical operator determines what will be rendered. The following code is from the examples in the official ReactJS documentation. It is one of the simplest examples of conditional rendering that I can think of.\n\n
我現在正在嘗試撰寫一個從字串中提取所有標題文本的正則運算式。markdown 中的標題以 # 開頭(可以是 1-6),在我的情況下總是以新行結尾。
我嘗試使用以下正則運算式,但在我的測驗字串上呼叫它不會回傳任何內容:
const testString = "\n# Clean-er ReactJS Code - Conditional Rendering\n\n## TL;DR\n\nMove render conditions into appropriately named variables. Abstract the condition logic into a function. This makes the render function code a lot easier to understand, refactor, reuse, test, and think about.\n\n## Introduction\n\nConditional rendering is when a logical operator determines what will be rendered. The following code is from the examples in the official ReactJS documentation. It is one of the simplest examples of conditional rendering that I can think of.\n\n"
const HEADING_R = /(?<!#)#{1,6} (.*?)(\\r(?:\\n)?|\\n)/gm;
const headings = HEADING_R.exec(content);
console.log('headings: ', headings);
此控制臺記錄headings為null(未找到匹配項)。我正在尋找的結果是:["# Clean-er ReactJS Code - Conditional Rendering", "## TL;DR", "## Introduction"].
我相信正則運算式是錯誤的,但不知道為什么。
uj5u.com熱心網友回復:
/#{1,6}. (?=\n)/g
#{1,6}... 匹配字符#至少一次或作為最多 6 個相等字符的序列。.匹配任何字符(行終止符除外)至少一次且盡可能多次(貪婪)這樣做直到積極的前瞻
(?=\n)匹配......- 這是……
\n換行符/換行符。
- 這是……
使用
g匹配所有內容的 lobal 修飾符。
編輯
提到過
“匹配任何字符(行終止符除外)”
因此,像 ... /#{1,6}. /g... 這樣的正則運算式應該已經為 OP 的用例完成了這項作業(不需要積極的前瞻),即 ...
“markdown 中的標題以 # 開頭(可以是 1-6),在我的情況下總是以新行結尾。 ”
我正在尋找的結果是:
["# Clean-er ReactJS Code - Conditional Rendering", "## TL;DR", "## Introduction"].
const testString = `\n# Clean-er ReactJS Code - Conditional Rendering\n\n## TL;DR\n\nMove render conditions into appropriately named variables. Abstract the condition logic into a function. This makes the render function code a lot easier to understand, refactor, reuse, test, and think about.\n\n## Introduction\n\nConditional rendering is when a logical operator determines what will be rendered. The following code is from the examples in the official ReactJS documentation. It is one of the simplest examples of conditional rendering that I can think of.\n\n`;
// see...[https://regex101.com/r/n6XQub/2]
const regXHeader = /#{1,6}. /g
console.log(
testString.match(regXHeader)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
獎金
將上述正則運算式重構為例如/(?<flag>#{1,6})\s (?<content>. )/g通過使用命名捕獲組以及ping 任務,matchAll可以獲得map類似于下一個提供的示例代碼計算的結果......
const testString = `\n# Clean-er ReactJS Code - Conditional Rendering\n\n## TL;DR\n\nMove render conditions into appropriately named variables. Abstract the condition logic into a function. This makes the render function code a lot easier to understand, refactor, reuse, test, and think about.\n\n## Introduction\n\nConditional rendering is when a logical operator determines what will be rendered. The following code is from the examples in the official ReactJS documentation. It is one of the simplest examples of conditional rendering that I can think of.\n\n`;
// see...[https://regex101.com/r/n6XQub/4]
const regXHeader = /(?<flag>#{1,6})\s (?<content>. )/g
console.log(
Array
.from(
testString.matchAll(regXHeader)
)
.map(({ groups: { flag, content } }) => ({
heading: `h${ flag.length }`,
content,
}))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
問題是您正在為正則運算式使用文字,并且不應雙重轉義反斜杠,因此您可以將其寫為 (?<!#)#{1,6} (.*?)(\r(?:\n)?|\n)
您可以縮短捕獲所需內容的模式并匹配尾隨換行符,而不是使用后向斷言。
(#{1,6} .*)\r?\n
檢索所有捕獲組 1 值:
const testString = "\n# Clean-er ReactJS Code - Conditional Rendering\n\n## TL;DR\n\nMove render conditions into appropriately named variables. Abstract the condition logic into a function. This makes the render function code a lot easier to understand, refactor, reuse, test, and think about.\n\n## Introduction\n\nConditional rendering is when a logical operator determines what will be rendered. The following code is from the examples in the official ReactJS documentation. It is one of the simplest examples of conditional rendering that I can think of.\n\n"
const HEADING_R = /(#{1,6} .*)\r?\n/g;
const headings = Array.from(testString.matchAll(HEADING_R), m => m[1]);
console.log('headings: ', headings);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418876.html
標籤:
