Разделить фрагмент уценки при появлении заголовков - PullRequest
2 голосов
/ 18 июня 2019

У меня есть следующий фрагмент уценки:

# 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 Ответ

2 голосов
/ 18 июня 2019

Используйте split с /^(?=#+ )/m регулярным выражением ( демо ) или сопоставьте с match(/^#+ [^#]*(?:#(?!#)[^#]*)*/gm) (см. другое демо ):

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)."
]

График регулярных выражений № 1 (расщепление) :

enter image description here

График регулярных выражений №2 (соответствует):

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...