Last Updated: May 13, 2022
·
8.94K
· gnclmorais

Regex to match GitHub’s Markdown code blocks

In a quest to fix a small issue on wtf.js, I struggled on the web for a few hours until I got a regular expression good enough for my purpose: match GitHub’s Markdown code blocks, which means code inside tags like ```.

/```[a-z]*\n[\s\S]*?\n```/g

The expression above is able to do the following (green highlights are matches):
Picture

Image courtesy of Refiddle.

3 Responses
Add your response

I think you hit an issue displaying your regex. Coderwall interpreted part of your regex as a code snippet and removed an important piece!

over 1 year ago ·

You are totally right, thank you! I had to change the post a bit, in order to render the regex correctly… Again, thank you!

over 1 year ago ·

Hello!
Thank you very much for your post!
I use your regex in python, but it doesn't work well.

It work for this :
sun.util.calendar.ZoneInfo[id="Europe/Zurich",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=119,lastRule=java.util.SimpleTimeZone[id=Europe/Zurich,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
But not for that :
java public void testMonthDayPlusDaysWrapsMonthCorrectly() { MonthDay d = new MonthDay(5,15); assertEquals(d.plusDays(25).plusDays(25), d.plusDays(50)); }
For exemple :
import re
string_1 = """ sun.util.calendar.ZoneInfo[id="Europe/Zurich",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=119,lastRule=java.util.SimpleTimeZone[id=Europe/Zurich,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]"""

string_2 = """java public void testMonthDayPlusDaysWrapsMonthCorrectly() { MonthDay d = new MonthDay(5,15); assertEquals(d.plusDays(25).plusDays(25), d.plusDays(50)); }"""

markdown1 = re.findall(r'([a-z]*\n[\s\S]*?\n)', string1)
markdown2 = re.findall(r'([a-z]*\n[\s\S]*?\n)', string2)

print(markdown1)
print(markdown
2)

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside