简体   繁体   English

使用javascript中的正则表达式在markdown中过滤列表标记

[英]filter list markup in markdown with regex in javascript

I have the following regex: \\^(?:[0-9]|\\-|\\+)[\\s\\S]*?\\n\\n\\gm , which should match only for list markups in markdown.我有以下正则表达式: \\^(?:[0-9]|\\-|\\+)[\\s\\S]*?\\n\\n\\gm ,它应该只匹配降价中的列表标记。 There are two problems remaining, first I get match with --- and second with ++text between++ .还有两个问题,首先我与---匹配,其次与++text between++匹配。

So, my Idea was something like this: \\^(?:[0-9]|\\-{1}?|\\+{1}?)[\\s\\S]*?\\n\\n\\gm to give only a match as return when there exist no repetition of + and - .所以,我的想法是这样的: \\^(?:[0-9]|\\-{1}?|\\+{1}?)[\\s\\S]*?\\n\\n\\gm给当+-不存在重复时,只有匹配作为返回。

But this way is not working.但这种方式行不通。

Check regex101 for details: https://regex101.com/r/Q3rA31/1详情请查看regex101: https ://regex101.com/r/Q3rA31/1

To be more specific: in markdown exists several possibilities to create lists, each of them I want to match with regex.更具体地说:在 Markdown 中存在多种创建列表的可能性,我想将每个列表与正则表达式匹配。 Unfortunately symbols like ++ and --- does match too which I want to avoid.不幸的是,像++---这样的符号也匹配,我想避免。

+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
  - Marker character change forces new list start:
    * Ac tristique libero volutpat at
    + Facilisis in pretium nisl aliquet
    - Nulla volutpat aliquam velit
+ Very easy!

1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa


1. You can use sequential numbers...
1. ...or keep all the numbers as `1.`

57. foo
1. bar

This pattern should do the trick:这种模式应该可以解决问题:

^[0-9+-]+[ .][\\s\\S]*?\\n{2}

https://regex101.com/r/Q3rA31/2 https://regex101.com/r/Q3rA31/2

I simplified the first part from your example, using a character set instead of alternation.我从你的例子中简化了第一部分,使用字符集而不是交替。 However, the big difference comes after that, where the pattern uses [ .] to match a dot or a space immediately after the list character, which seems to be a requirement in markdown syntax.然而,最大的不同在于,模式使用[ .]来匹配列表字符后面的点或空格,这似乎是 Markdown 语法的要求。

A simple way to solve this without changing your original very much is to add a negative lookahead after your - and + regex to make sure they aren't followed by duplicates of themselves:解决此问题的一种简单方法是在您的-+正则表达式之后添加一个负前瞻,以确保它们后面没有重复的自己:

/^(?:[0-9]|\-(?!\-)|\+(?!\+))[\s\S]*?\n\n/mg

https://regex101.com/r/Q3rA31/3 https://regex101.com/r/Q3rA31/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM