标头出现时拆分markdown代码段

时间:2019-06-18 06:38:03

标签: javascript regex markdown

我有以下标记降价片段:

# Glossary

This guide is aimed to familiarize the users with definitions to relevant DVC
concepts and terminologies which are frequently used.

## Workspace directory

Also abbreviated as workspace, it is the root directory of a project where DVC
is initialized by running `dvc init` command. Therefore, this directory will
contain a `.dvc` directory as well.

## Cache directory

DVC cache is a hidden storage which is found at `.dvc/cache`. This storage is
used to manage different versions of files which are under DVC control. For more
information on cache, please refer to the this
[guide](/doc/commands-reference/config#cache).

我想将其拆分为可以匹配的内容:

# Glossary
...
## Workspace directory
...
## Cache directory
...

我尝试使用正则表达式/#{1,2}\s.+\n{2}[^(#{2}\s)]*/来匹配它们。我的意图是首先将标题与此部分#{1,2}\s.+\n{2}匹配,然后在找到##\s时终止匹配。但是我在第二部分中失败了。谁能指导我?

1 个答案:

答案 0 :(得分:2)

split与正则表达式/^(?=#+ )/mdemo)配合使用或与match(/^#+ [^#]*(?:#(?!#)[^#]*)*/gm)匹配(请参阅another demo):

let contents = `# Glossary

This guide is aimed to familiarize the users with definitions to relevant DVC
concepts and terminologies which are frequently used.

## Workspace directory

Also abbreviated as workspace, it is the root directory of a project where DVC
is initialized by running \`dvc init\` command. Therefore, this directory will
contain a \`.dvc\` directory as well.

## Cache directory

DVC cache is a hidden storage which is found at \`.dvc/cache\`. This storage is
used to manage different versions of files which are under DVC control. For more
information on cache, please refer to the this
[guide](/doc/commands-reference/config#cache).`;

console.log(contents.split(/^(?=#+ )/m).filter(Boolean));
console.log(contents.match(/^#+ [^#]*(?:#(?!#)[^#]*)*/gm));

输出:

[
  "# Glossary\n\nThis guide is aimed to familiarize the users with definitions to relevant DVC\nconcepts and terminologies which are frequently used.\n\n",
  "## Workspace directory\n\nAlso abbreviated as workspace, it is the root directory of a project where DVC\nis initialized by running `dvc init` command. Therefore, this directory will\ncontain a `.dvc` directory as well.\n\n",
  "## Cache directory\n\nDVC cache is a hidden storage which is found at `.dvc/cache`. This storage is\nused to manage different versions of files which are under DVC control. For more\ninformation on cache, please refer to the this\n[guide](/doc/commands-reference/config#cache)."
]

Regex #1 (splitting) graph

enter image description here

正则表达式2(匹配)图:

enter image description here