Top 50 Useful Regex Patterns
Tags: javascript, webdev, beginners, programming
Published: 2024-02-13 12:56:28

**Mastering Regular Expressions in JavaScript: Top 50 Useful Regex Patterns**

> Know More :- https://codexdindia.blogspot.com/2024/02/top-50-useful-regex-patterns.html
> Join Discord 🚀 :- https://discord.gg/26DFm2rku9


**Introduction:**

Regular expressions (regex) are a powerful tool for pattern matching and text manipulation in JavaScript. Whether you're validating user input, extracting data from strings, or replacing text, regex can streamline your coding tasks. In this article, we'll explore 50 useful regex patterns that every JavaScript developer should know. These regex patterns cover a wide range of common scenarios, from validating email addresses to parsing URLs and beyond.

**1. Validating Email Addresses:**
```javascript
const emailRegex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
```

**2. Validating URLs:**
```javascript
const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
```

**3. Validating Dates in YYYY-MM-DD Format:**
```javascript
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
```

**4. Validating Phone Numbers (US Format):**
```javascript
const phoneRegex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
```

**5. Validating Postal Codes (US Format):**
```javascript
const postalCodeRegex = /^\d{5}(?:-\d{4})?$/;
```

**6. Validating Credit Card Numbers (Luhn Algorithm):**
```javascript
const creditCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;
```

**7. Validating Social Security Numbers (SSN):**
```javascript
const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;
```

**8. Validating IP Addresses (IPv4):**
```javascript
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
```

**9. Validating Hexadecimal Color Codes:**
```javascript
const hexColorRegex = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
```

**10. Extracting All URLs from a String:**
```javascript
const extractURLs = /https?:\/\/\S+/g;
```

**11. Extracting All Email Addresses from a String:**
```javascript
const extractEmails = /[\w.-]+@[a-zA-Z-]+\.[a-zA-Z]{2,3}/g;
```

**12. Matching HTML Tags:**
```javascript
const htmlTagsRegex = /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/gi;
```

**13. Matching HTML Comments:**
```javascript
const htmlCommentsRegex = /<!--[\s\S]*?-->/g;
```

**14. Matching CSS Classes:**
```javascript
const cssClassRegex = /\.([a-zA-Z_-][\w-]*)/g;
```

**15. Matching Twitter Handles:**
```javascript
const twitterHandleRegex = /@[a-zA-Z0-9_]+/g;
```

**16. Matching Hashtags:**
```javascript
const hashtagRegex = /#[a-zA-Z0-9_]+/g;
```

**17. Matching Mentions in Tweets:**
```javascript
const mentionRegex = /@([a-zA-Z0-9_]+)/g;
```

**18. Matching YouTube Video IDs in URLs:**
```javascript
const youtubeVideoIdRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
```

**19. Matching Twitter URLs:**
```javascript
const twitterUrlRegex = /(?:https?:\/\/)?(?:www\.)?twitter\.com\/(#!\/)?[a-zA-Z0-9_]+/;
```

**20. Matching YouTube URLs:**
```javascript
const youtubeUrlRegex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
```

**21. Matching Instagram Usernames:**
```javascript
const instagramUsernameRegex = /(?:https?:\/\/)?(?:www\.)?instagram\.com\/[a-zA-Z0-9_]+/;
```

**22. Matching Instagram URLs:**
```javascript
const instagramUrlRegex = /(?:https?:\/\/)?(?:www\.)?instagram\.com\/(p|tv|reel)\/[a-zA-Z0-9_]+/;
```

**23. Matching YouTube Playlist URLs:**
```javascript
const youtubePlaylistRegex = /(?:https?:\/\/)?(?:www\.)?youtube\.com\/playlist\?list=([a-zA-Z0-9_-]+)/;
```

**24. Matching Google Drive URLs:**
```javascript
const googleDriveUrlRegex = /(?:https?:\/\/)?drive\.google\.com\/(?:file\/d\/|open\?id=)([a-zA-Z0-9_-]+)/;
```

**25. Matching SoundCloud URLs:**
```javascript
const soundcloudUrlRegex = /(?:https?:\/\/)?(?:www\.)?soundcloud\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+/;
```

**26. Matching GitHub Repository URLs:**
```javascript
const githubRepoUrlRegex = /(?:https?:\/\/)?(?:www\.)?github\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+/;
```

**27. Matching GitHub Gist URLs:**
```javascript
const githubGistUrlRegex = /(?:https?:\/\/)?(?:www\.)?gist\.github\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9]+/;
```



**28. Matching Medium URLs:**
```javascript
const mediumUrlRegex = /(?:https?:\/\/)?(?:www\.)?medium\.com\/(@[a-zA-Z0-9_-]+)\/[a-zA-Z0-9_-]+/;
```

**29. Matching Stack Overflow URLs:**
```javascript
const stackoverflowUrlRegex = /(?:https?:\/\/)?(?:www\.)?stackoverflow\.com\/questions\/([0-9]+)/;
```

**30. Matching LinkedIn Profile URLs:**
```javascript
const linkedinProfileUrlRegex = /(?:https?:\/\/)?(?:www\.)?linkedin\.com\/in\/[a-zA-Z0-9_-]+/;
```

**31. Matching IPv6 Addresses:**
```javascript
const ipv6Regex = /(?:(?:(?:[0-9a-fA-F]{1,4}:){6}|(?=(?:[0-9a-fA-F]{0,4}:){2,6}(?:[0-9.]{1,3})){((?=.*(::))(?=(.*?::))(::)?(([0-9a-fA-F]{1,4}:){0,5}|:)((?::[0-9a-fA-F]{1,4}){1,2}:|(?:[0-9]{1,3}\.){3}[0-9]{1,3})))(%.+)?\b/;
```

**32. Matching Base64 Encoded Strings:**
```javascript
const base64Regex = /^[a-zA-Z0-9-_]+={0,2}$/;
```

**33. Matching Markdown Headings:**
```javascript
const markdownHeadingRegex = /^(#{1,6})\s(.+)/;
```

**34. Matching Markdown Links:**
```javascript
const markdownLinkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
```

**35. Matching HTML Entities:**
```javascript
const htmlEntityRegex = /&(?:[a-zA-Z]+|#\d+);/;
```

**36. Matching XML Tags:**
```javascript
const xmlTagRegex = /<([a-zA-Z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/gi;
```

**37. Matching Docker Image Tags:**
```javascript
const dockerTagRegex = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
```

**38. Matching Semantic Versioning (SemVer):**
```javascript
const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)?(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
```

**39. Matching YAML Front Matter:**
```javascript
const yamlFrontMatterRegex = /---\n([\s\S]*?)\n---/;
```

**40. Matching JSON Objects:**
```javascript
const jsonObjectRegex = /{[\s\S]*?}/;
```

**41. Matching JSON Arrays:**
```javascript
const jsonArrayRegex = /\[[\s\S]*?\]/;
```

**42. Matching CSS Color Codes:**
```javascript
const cssColorRegex = /#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})/;
```

**43. Matching HTML Hexadecimal Entities:**
```javascript
const htmlHexEntityRegex = /&#x[0-9A-Fa-f]+;/;
```

**44. Matching HTML Decimal Entities:**
```javascript
const htmlDecimalEntityRegex = /&#\d+;/;
```

**45. Matching Base64 Data URLs:**
```javascript
const base64DataUrlRegex = /^data:([a-z]+)\/([a-z]+);base64,([^\s]+)/;
```

**46. Matching UUID (Universally Unique Identifier):**
```javascript
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
```

**47. Matching ISBN (International Standard Book Number):**
```javascript
const isbnRegex = /^(?:ISBN(?:-10)?:? ?)?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?(?:[0-9]+[- ]?){2}[0-9X]$/;
```

**48. Matching XML or HTML Comments:**
```javascript
const xmlHtmlCommentRegex = /<!--[\s\S]*?-->/;
```

**49. Matching Google Analytics Tracking IDs:**
```javascript
const googleAnalyticsIdRegex = /^(UA|YT|MO)-\d{4,}-\d{1,2}$/;
```

**50. Matching ISBN (International Standard Book Number) with Hyphens:**
```javascript
const isbnWithHyphensRegex = /^(?=(?:[0-9]+[- ]){3})[- 0-9]{13}$/;
```

**Conclusion:**

Regular expressions are an indispensable tool for working with text in JavaScript. By mastering these 50 useful regex patterns, you can handle a wide range of tasks efficiently, from validating user input to extracting data from strings and more. Experiment with these regex patterns in your projects and adapt them as needed to suit your specific requirements. Happy coding!