動作確認:見出しを表す正規表現とマッチする箇所
code:1行目が見出しでない.js
const text = `ほげ
# s
ふが
## t
ぴよ`;
const titleRegex = /^#+\s/g;
const regex = new RegExp(titleRegex);
let matches;
while ((matches = regex.exec(text)) !== null) {
console.log(Found ${matches[0]}. Index of matches: ${matches.index}. Next starts at ${regex.lastIndex})
}
console.log("End");
"End"とだけ表示される
code:1行目を見出しに変更.js
const text = `# s
ふが
## t
ぴよ`;
// 以下は共通なので省略
"Found # . Index of matches: 0. Next starts at 2"が表示されるようになった
## tは表示されない。これは/^#+\s/g;(文字列の先頭)にマッチしていないから
code:1行目はどんなレベルの見出しでもマッチする
const text = `## t
# s
ぴよ`;
"Found ## . Index of matches: 0. Next starts at 3"