Skip to main content

Top 50 Useful Regex Patterns

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

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:

const emailRegex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
JavaScript

2. Validating URLs:

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

3. Validating Dates in YYYY-MM-DD Format:

const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
JavaScript

4. Validating Phone Numbers (US Format):

const phoneRegex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
JavaScript

5. Validating Postal Codes (US Format):

const postalCodeRegex = /^\d{5}(?:-\d{4})?$/;
JavaScript

6. Validating Credit Card Numbers (Luhn Algorithm):

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})$/;
JavaScript

7. Validating Social Security Numbers (SSN):

const ssnRegex = /^\d{3}-\d{2}-\d{4}$/;
JavaScript

8. Validating IP Addresses (IPv4):

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]?)$/;
JavaScript

9. Validating Hexadecimal Color Codes:

const hexColorRegex = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
JavaScript

10. Extracting All URLs from a String:

const extractURLs = /https?:\/\/\S+/g;
JavaScript

11. Extracting All Email Addresses from a String:

const extractEmails = /[\w.-]+@[a-zA-Z-]+\.[a-zA-Z]{2,3}/g;
JavaScript

12. Matching HTML Tags:

const htmlTagsRegex = /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/gi;
JavaScript

13. Matching HTML Comments:

const htmlCommentsRegex = /<!--[\s\S]*?-->/g;
JavaScript

14. Matching CSS Classes:

const cssClassRegex = /\.([a-zA-Z_-][\w-]*)/g;
JavaScript

15. Matching Twitter Handles:

const twitterHandleRegex = /@[a-zA-Z0-9_]+/g;
JavaScript

16. Matching Hashtags:

const hashtagRegex = /#[a-zA-Z0-9_]+/g;
JavaScript

17. Matching Mentions in Tweets:

const mentionRegex = /@([a-zA-Z0-9_]+)/g;
JavaScript

18. Matching YouTube Video IDs in URLs:

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

19. Matching Twitter URLs:

const twitterUrlRegex = /(?:https?:\/\/)?(?:www\.)?twitter\.com\/(#!\/)?[a-zA-Z0-9_]+/;
JavaScript

20. Matching YouTube URLs:

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

21. Matching Instagram Usernames:

const instagramUsernameRegex = /(?:https?:\/\/)?(?:www\.)?instagram\.com\/[a-zA-Z0-9_]+/;
JavaScript

22. Matching Instagram URLs:

const instagramUrlRegex = /(?:https?:\/\/)?(?:www\.)?instagram\.com\/(p|tv|reel)\/[a-zA-Z0-9_]+/;
JavaScript

23. Matching YouTube Playlist URLs:

const youtubePlaylistRegex = /(?:https?:\/\/)?(?:www\.)?youtube\.com\/playlist\?list=([a-zA-Z0-9_-]+)/;
JavaScript

24. Matching Google Drive URLs:

const googleDriveUrlRegex = /(?:https?:\/\/)?drive\.google\.com\/(?:file\/d\/|open\?id=)([a-zA-Z0-9_-]+)/;
JavaScript

25. Matching SoundCloud URLs:

const soundcloudUrlRegex = /(?:https?:\/\/)?(?:www\.)?soundcloud\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+/;
JavaScript

26. Matching GitHub Repository URLs:

const githubRepoUrlRegex = /(?:https?:\/\/)?(?:www\.)?github\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+/;
JavaScript

27. Matching GitHub Gist URLs:

const githubGistUrlRegex = /(?:https?:\/\/)?(?:www\.)?gist\.github\.com\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9]+/;
JavaScript

28. Matching Medium URLs:

const mediumUrlRegex = /(?:https?:\/\/)?(?:www\.)?medium\.com\/(@[a-zA-Z0-9_-]+)\/[a-zA-Z0-9_-]+/;
JavaScript

29. Matching Stack Overflow URLs:

const stackoverflowUrlRegex = /(?:https?:\/\/)?(?:www\.)?stackoverflow\.com\/questions\/([0-9]+)/;
JavaScript

30. Matching LinkedIn Profile URLs:

const linkedinProfileUrlRegex = /(?:https?:\/\/)?(?:www\.)?linkedin\.com\/in\/[a-zA-Z0-9_-]+/;
JavaScript

31. Matching IPv6 Addresses:

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/;
JavaScript

32. Matching Base64 Encoded Strings:

const base64Regex = /^[a-zA-Z0-9-_]+={0,2}$/;
JavaScript

33. Matching Markdown Headings:

const markdownHeadingRegex = /^(#{1,6})\s(.+)/;
JavaScript

34. Matching Markdown Links:

const markdownLinkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
JavaScript

35. Matching HTML Entities:

const htmlEntityRegex = /&(?:[a-zA-Z]+|#\d+);/;
JavaScript

36. Matching XML Tags:

const xmlTagRegex = /<([a-zA-Z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/gi;
JavaScript

37. Matching Docker Image Tags:

const dockerTagRegex = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
JavaScript

38. Matching Semantic Versioning (SemVer):

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-]+)*))?$/;
JavaScript

39. Matching YAML Front Matter:

const yamlFrontMatterRegex = /---\n([\s\S]*?)\n---/;
JavaScript

40. Matching JSON Objects:

const jsonObjectRegex = /{[\s\S]*?}/;
JavaScript

41. Matching JSON Arrays:

const jsonArrayRegex = /\[[\s\S]*?\]/;
JavaScript

42. Matching CSS Color Codes:

const cssColorRegex = /#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})/;
JavaScript

43. Matching HTML Hexadecimal Entities:

const htmlHexEntityRegex = /&#x[0-9A-Fa-f]+;/;
JavaScript

44. Matching HTML Decimal Entities:

const htmlDecimalEntityRegex = /&#\d+;/;
JavaScript

45. Matching Base64 Data URLs:

const base64DataUrlRegex = /^data:([a-z]+)\/([a-z]+);base64,([^\s]+)/;
JavaScript

46. Matching UUID (Universally Unique Identifier):

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}$/;
JavaScript

47. Matching ISBN (International Standard Book Number):

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]$/;
JavaScript

48. Matching XML or HTML Comments:

const xmlHtmlCommentRegex = /<!--[\s\S]*?-->/;
JavaScript

49. Matching Google Analytics Tracking IDs:

const googleAnalyticsIdRegex = /^(UA|YT|MO)-\d{4,}-\d{1,2}$/;
JavaScript

50. Matching ISBN (International Standard Book Number) with Hyphens:

const isbnWithHyphensRegex = /^(?=(?:[0-9]+[- ]){3})[- 0-9]{13}$/;
JavaScript

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!

Comments

Popular posts from this blog

How to Get Free Unlimited Bandwidth and Storage Using jsDelivr and GitHub

How to Get Free Unlimited Bandwidth and Storage Using jsDelivr and GitHub Are you tired of paying for expensive content delivery networks (CDNs) and storage solutions for your web projects? Look no further! In this guide, we'll show you how to leverage jsDelivr and GitHub to get free unlimited bandwidth and storage. Whether you're a seasoned developer or just getting started, this solution will save you money and improve the performance of your web projects. What is jsDelivr? jsDelivr is a free, fast, and reliable CDN for open-source files. It provides a convenient way to serve your static assets (like JavaScript, CSS, images, and more) with the benefits of a global CDN, including faster load times and unlimited bandwidth. What is GitHub? GitHub is a popular platform for version control and collaboration. It allows you to host your code repositories and manage your projects with ease. By combining GitHub with jsD...

Best VS Code extensions for developers in 2024

Here are some of the best VS Code extensions for developers in 2024, including a range of productivity tools, debuggers, and visual enhancements to streamline your coding workflow. Additionally, you'll find some popular themes to customize your editor's appearance. Top VS Code Extensions for Developers in 2024 Shade Theme by SH20RAJ Enhance your code readability with this well-designed theme, perfect for long coding sessions. Shade Theme Prettier A widely used code formatter that ensures your code is styled consistently across your projects. Prettier GitLens Provides rich visualizations and insights into your Git repository, helping you understand code changes and history. GitLens Auto Rename Tag Automatically renames paired HTML/XML tags, reducing errors and saving time. Auto Rename Tag Bracket Pair Colorizer Colors matching brackets to improve code readability, especially useful for complex nested structures. Bracket Pair Colorizer CSS Peek...

Random Posts