<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sarthak20241</title>
    <description>The latest articles on DEV Community by sarthak20241 (@sarthak20241).</description>
    <link>https://dev.to/sarthak20241</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3936197%2F220e7a10-29b6-44ed-aa6f-81c190509f52.png</url>
      <title>DEV Community: sarthak20241</title>
      <link>https://dev.to/sarthak20241</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarthak20241"/>
    <language>en</language>
    <item>
      <title>A regex cheatsheet of the patterns I actually use weekly</title>
      <dc:creator>sarthak20241</dc:creator>
      <pubDate>Sun, 17 May 2026 12:43:13 +0000</pubDate>
      <link>https://dev.to/sarthak20241/a-regex-cheatsheet-of-the-patterns-i-actually-use-weekly-4j1a</link>
      <guid>https://dev.to/sarthak20241/a-regex-cheatsheet-of-the-patterns-i-actually-use-weekly-4j1a</guid>
      <description>&lt;p&gt;I keep googling the same regex patterns. Here are the ones I actually need, with explanations, gotchas, and test cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match a valid email (loose, RFC-incompliant but practical)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^[^\s@]+@[^\s@]+\.[^\s@]+$&lt;/code&gt;Matches anything that looks like an email without trying to validate RFC 5322 (which is famously impossible with regex alone). For real validation, send a confirmation email.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slugify a string
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;[^a-z0-9]+&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Replace matches with &lt;code&gt;-&lt;/code&gt;, then trim leading/trailing dashes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[^&lt;/span&gt;&lt;span class="sr"&gt;a-z0-9&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;+/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/^-+|-+$/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; "hello-world"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Match a UUID v4
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;4&lt;/code&gt; at position 13 is the version marker. The first character of the fourth group must be 8, 9, a, or b (that's the variant).&lt;/p&gt;

&lt;h2&gt;
  
  
  Strip HTML tags (for preview, NOT for sanitization)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;[^&amp;gt;]+&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Fine for "give me a plain-text preview of this HTML." Absolutely NOT fine for sanitizing user input. Use DOMPurify for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match an ISO date (YYYY-MM-DD)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^\d{4}-\d{2}-\d{2}$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Doesn't check that the date is valid (matches 2024-13-99) but catches malformed input.&lt;/p&gt;

&lt;h2&gt;
  
  
  Capture markdown links
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;\[([^\]]+)\]\(([^)]+)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Group 1 is the link text, group 2 is the URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Match a US phone number (lenient)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Matches 555-555-5555, (555) 555-5555, +1 555.555.5555, 5555555555.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common gotchas I always forget
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Greedy vs lazy
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.*&lt;/code&gt; matches as much as possible. &lt;code&gt;.*?&lt;/code&gt; matches as little as possible. If you're capturing inside quotes, you want lazy: &lt;code&gt;"(.*?)"&lt;/code&gt;. Greedy &lt;code&gt;"(.*)"&lt;/code&gt; will match across multiple strings on the same line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Anchors and multiline mode
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;^&lt;/code&gt; and &lt;code&gt;$&lt;/code&gt; match the start and end of the &lt;em&gt;string&lt;/em&gt; by default, not the start and end of each &lt;em&gt;line&lt;/em&gt;. Add the &lt;code&gt;m&lt;/code&gt; flag to make them match line boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Escaping inside character classes
&lt;/h3&gt;

&lt;p&gt;Inside &lt;code&gt;[ ]&lt;/code&gt;, most special characters don't need escaping. The ones that do: &lt;code&gt;]&lt;/code&gt;, &lt;code&gt;\&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt; (only if first), &lt;code&gt;-&lt;/code&gt; (only if between two characters). So &lt;code&gt;[.+*?]&lt;/code&gt; matches a literal dot, plus, asterisk, or question mark. No backslashes needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Capturing vs non-capturing
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;(foo)&lt;/code&gt; captures. &lt;code&gt;(?:foo)&lt;/code&gt; matches but doesn't capture. Use non-capturing when you only need the grouping for repetition or alternation: &lt;code&gt;(?:https?|ftp)://&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A small free tool
&lt;/h2&gt;

&lt;p&gt;If you don't want to memorize any of this, I built a regex generator that takes plain English ("match a URL with optional protocol") and gives back the regex with test cases: &lt;a href="https://tinkrtool.com/regex" rel="noopener noreferrer"&gt;tinkrtool.com/regex&lt;/a&gt;. Free, no signup. But the patterns above cover 80% of what I actually need.&lt;/p&gt;

&lt;h2&gt;
  
  
  What patterns do you actually use?
&lt;/h2&gt;

&lt;p&gt;Drop your daily-driver regexes in the comments. Bonus points if yours has a gotcha you only learned about in production.&lt;/p&gt;

</description>
      <category>regex</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
