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):

Image courtesy of Refiddle.
Written by Gonçalo Morais
Related protips
3 Responses
I think you hit an issue displaying your regex. Coderwall interpreted part of your regex as a code snippet and removed an important piece!
You are totally right, thank you! I had to change the post a bit, in order to render the regex correctly… Again, thank you!
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(markdown2)
Have a fresh tip? Share with Coderwall community!
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