<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[markdown - Stapps.io]]></title><description><![CDATA[markdown - Stapps.io]]></description><link>https://blog.stapps.io/</link><generator>Ghost 0.11</generator><lastBuildDate>Thu, 01 Jan 2026 22:01:57 GMT</lastBuildDate><atom:link href="https://blog.stapps.io/tag/markdown/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Match nth case with regex]]></title><description><![CDATA[<p>For an app I worked on lately I implemented some Github style checkboxes in their markdown syntax. </p>

<p>The format is like so:</p>

<pre><code>[ ] Checkbox 1
[ ] Checkbox 2
[ ] Checkbox 3
</code></pre>

<p><em>There's a couple ways you could approach this but I've gone with regular expression.</em></p>

<p>The pattern to match this is: <code>((\[[ x]].*?(\r\</code></p>]]></description><link>https://blog.stapps.io/match-nth-case-with-regular-expression/</link><guid isPermaLink="false">d2647153-b24b-4e85-a268-524c4528d4a6</guid><category><![CDATA[Development]]></category><category><![CDATA[regex]]></category><category><![CDATA[markdown]]></category><dc:creator><![CDATA[Andrew Stilliard]]></dc:creator><pubDate>Wed, 13 Jul 2016 20:19:55 GMT</pubDate><content:encoded><![CDATA[<p>For an app I worked on lately I implemented some Github style checkboxes in their markdown syntax. </p>

<p>The format is like so:</p>

<pre><code>[ ] Checkbox 1
[ ] Checkbox 2
[ ] Checkbox 3
</code></pre>

<p><em>There's a couple ways you could approach this but I've gone with regular expression.</em></p>

<p>The pattern to match this is: <code>((\[[ x]].*?(\r\n|\r|\n)){INDEX})\[[ x]](.*)</code> <br>
Where INDEX is the nth we want to match -1. If we want the first, the index would be 0. 2nd would be 1, 3rd would be 2 etc. <br>
E.g. to match the 2nd, the INDEX would be 1: <code>((\[[ x]].*?(\r\n|\r|\n)){1})\[[ x]](.*)</code></p>

<p>Then we can use <code>$1[x]$4</code> as the replacement.</p>

<p>Replacing the 2nd would result in:  </p>

<pre><code>[ ] Checkbox 1
[x] Checkbox 2
[ ] Checkbox 3
</code></pre>

<p>But let's break this pattern down. The first half of this is in brackets representing the first captured group as $1 for the replace statement. In this we have <code>\[[ x]]</code> which matches square brackets an empty space or x inside, then <code>.*?</code> matches any content after it till the end of the line, followed by <code>(\r\n|\r|\n)</code> which detects the end of the line. This then get's repeated for the INDEX <code>{INDEX}</code> or <code>{1}</code> for matching the 2nd checkbox. The INDEX is 1 less than the value we are looking for as it starts at 0 rather than 1. This ends the first capturing group. We then we look for the checkbox again <code>\[[ x]]</code> as this is the one we will be replacing and grab all the content after it in a last capturing group which in the replace is $4. </p>

<p>Try this out at <a href="http://regexr.com/3dqac">http://regexr.com/3dqac</a> <br>
Use the original empty checkboxes text above and the regex, then click to replace and enter the replacement script. Then try changing the index between 0, 1 and 2 to see if live replace whichever checkbox.</p>]]></content:encoded></item></channel></rss>