Regular Expressions 101

Community Patterns

Matching un-wrapped HTML & CSS code blocks in Markdown

-1

Regular Expression
ECMAScript (JavaScript)

/
(?<!```html\s*)(<link href=.*?)(?=^\s*$)
/
gmis

Description

PATTERNS

RegEx Flavour

ECMAScript (JavaScript, ActionScript, TypeScript, etc.)

NOTES FOR ALL PATTERNS IN THIS SCRIPT:

  • These require the regex library, not Python's built-in-crap
  • These require the Single Line (regex101) or regex.DOTALL (Python regex) flag

Style Blocks (2 patterns)

Matches:

  • All <style> any code here </style> blocks that aren't preceded by ```css + any amount of optional whitespace

Pattern 1: Old version (would have replaced all punctuation with punctuation from the string library)

<font color=red>NOTE</font>: Does not require regex.DOTALL

    (?<!```css\s*)(\<style\>[\sa-z0-9\-\.\[\{\]\}:;#]+\<\/style\>)

Pattern 2: Newest version (much better)

    (?<!```css\s*)(<style>.*?<\/style>)
    (?<!```css\s*)(<style>.*?<\/style>)

HTML Document

Matches:

  • This does the same thing as above but for <!DOCTYPE html> any code here </html>

Pattern:

    (?<!```html\s*)(<!DOCTYPE html>.*?<\/html>)

HTML Link

Matches:

  • All blocks of code containing <link href=> up to, but not including, an empty line

Pattern:

    (?<!```html\s*)(<link href=.*?)(?=^\s*$)
Submitted by anonymous - 10 months ago