简体   繁体   中英

How to modify this regex so it doesn't match * * *

(?<!\*)\*([^*]+?)\*(?!\*)/g

This is basically a regex to match Markdown italics. It will match the following situations:

This *is* Markdown.
This *is Markdown.*
*This* is *Markdown.*
*This is Markdown.*

Note: The negatives lookarounds are there to avoid matching cases like this one This is **Markdown.**

It works ... almost. It's also matching * * * , which I use as section breaks. Like this:

* * *

This *is* Markdown.

* * *

How to change my regex so it doesn't match these section breaks? I'm really stuck.

RegExr: https://regexr.com/4tv5n

According to the documentation there are more valid horizontal rule combinations.

This one I think is simpler and better captures the essence of the rule :

\*(?!\s*\*)([^*\n]+)\*

It is also faster, as it takes 89 steps instead of 204 steps .

This may suffice (?<!\\*)\\*([^ ][^*]*?)\\*(?!\\*) . For the example at least.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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