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

2. Validating URLs:

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

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

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

4. Validating Phone Numbers (US Format):

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

5. Validating Postal Codes (US Format):

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

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

7. Validating Social Security Numbers (SSN):

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

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

9. Validating Hexadecimal Color Codes:

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

10. Extracting All URLs from a String:

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

11. Extracting All Email Addresses from a String:

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

12. Matching HTML Tags:

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

13. Matching HTML Comments:

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

14. Matching CSS Classes:

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

15. Matching Twitter Handles:

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

16. Matching Hashtags:

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

17. Matching Mentions in Tweets:

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

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

19. Matching Twitter URLs:

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

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

21. Matching Instagram Usernames:

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

22. Matching Instagram URLs:

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

23. Matching YouTube Playlist URLs:

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

24. Matching Google Drive URLs:

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

25. Matching SoundCloud URLs:

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

26. Matching GitHub Repository URLs:

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

27. Matching GitHub Gist URLs:

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

28. Matching Medium URLs:

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

29. Matching Stack Overflow URLs:

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

30. Matching LinkedIn Profile URLs:

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

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

32. Matching Base64 Encoded Strings:

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

33. Matching Markdown Headings:

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

34. Matching Markdown Links:

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

35. Matching HTML Entities:

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

36. Matching XML Tags:

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

37. Matching Docker Image Tags:

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

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

39. Matching YAML Front Matter:

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

40. Matching JSON Objects:

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

41. Matching JSON Arrays:

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

42. Matching CSS Color Codes:

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

43. Matching HTML Hexadecimal Entities:

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

44. Matching HTML Decimal Entities:

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

45. Matching Base64 Data URLs:

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

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

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

48. Matching XML or HTML Comments:

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

49. Matching Google Analytics Tracking IDs:

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

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

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!

Comments

Popular posts from this blog

How to Add a VS Code Editor to Your Website

How to Add a VS Code Editor to Your Website The Monaco editor by Microsoft provides a code editor component that can be easily integrated into websites. With just a few lines of code, you can add a full-featured editor similar to VS Code in your web app. In this tutorial, we'll see how to do just that. Getting Started To use Monaco, we need to include it in our page. We can get it from a CDN: < script src = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs/loader.min.js" > </ script > This will load the Monaco library asynchronously. Next, we need a <div> in our HTML where we can instantiate the editor: < div id = "editor" ></ div > Now in our JavaScript code, we can initialize Monaco and create the editor: require .config({ paths : { 'vs' : 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs' }}); require ([ "vs/editor/editor.main" ], function ( ) { const ...

10 Free GitHub Copilot Alternatives for VS Code in 2024

10 Free GitHub Copilot Alternatives for VS Code in 2024 As developers, we're always on the lookout for tools that can help us write code more efficiently. GitHub Copilot has been a game-changer in this regard, but its premium pricing may be a deterrent for some. Fortunately, there are several free alternatives available that offer similar functionality. In this article, we'll explore 10 of the best free GitHub Copilot alternatives for Visual Studio Code in 2024. Comparison of Free GitHub Copilot Alternatives Tool Language Support Auto-Completion Code Generation Code Explanation Bito Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✓ Tabnine Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✗ Amazon CodeWhisperer Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP ✓ ✓ ✗ Codeium Python, JavaScript, TypeScript, Java, C#, C...

Top React UI Libraries ๐ŸŒŸ

๐ŸŒŸ The Ultimate Roundup of Top React UI Libraries for Your Next Project! ๐Ÿš€๐ŸŽจ Hey there, React wizards! ๐Ÿช„✨ Ready to take your frontend game to the next level? Let's dive into an even broader spectrum of incredible React UI libraries that'll make your interfaces shine like never before! ๐Ÿ’ป๐ŸŒˆ 1. Tremor UI ๐ŸŒŠ ๐ŸŒŸ Tremor UI is a rising star in the React UI galaxy! ✨ It offers a sleek and modern design language, perfect for crafting stylish buttons and more. ๐Ÿ”˜๐ŸŽจ With Tremor, you can effortlessly create eye-catching user interfaces with its intuitive API and customizable components. ๐Ÿช„✨ Key Features : Modern Design Aesthetic Easy Customization Focus on User Experience 2. Radix UI ๐ŸŒฑ ๐ŸŒŸ Radix UI is all about building accessible, powerful components for React. ๐Ÿ› ️๐Ÿ”ฉ From modals to tooltips, Radix UI provides a solid foundation for creating interactive and user-friendly interfaces. ๐ŸŒ๐Ÿงก Dive into Radix ...

Random Posts