Skip to content

Instantly share code, notes, and snippets.

@yourvza
Created July 12, 2024 07:45
Show Gist options
  • Save yourvza/998e0ab3ba1d6d02d0deb5a4a7a8bac5 to your computer and use it in GitHub Desktop.
Save yourvza/998e0ab3ba1d6d02d0deb5a4a7a8bac5 to your computer and use it in GitHub Desktop.

AnorherRegexTutorial

Regular expressions are a powerful and versatile tool used for pattern matching and string manipulation in many programming languages and text editors. This tutorial covers the syntax and basic concepts of regex, including anchors, quantifiers, character classes, and more.

Summary

Briefly summarize the regex you will be describing and what you will explain. Include a code snippet of the regex. Replace this text with your summary.

Table of Contents

Regex Components

Anchors

Anchors are used to match patterns at the beginning or end of a line, word, or string. The ^ character matches the beginning of a line, while the $ character matches the end of a line.

/^Start/  // Matches any string that starts with "Start"
/End$/    // Matches any string that ends with "End"

Quantifiers

Quantifiers specify how many times a pattern should be repeated. The * quantifier matches zero or more occurrences of the preceding character, while the + quantifier matches one or more occurrences.

/\w*/  // Matches any string with zero or more word characters
/a+/   // Matches any string that contains one or more "a" characters

Grouping Constructs

Parentheses are used to group parts of a regular expression together and capture the matched text.

/(dog|cat)s?/  // Matches "dog", "dogs", "cat", or "cats"

Bracket Expressions

Bracket expressions allow you to match a range of characters, such as all uppercase letters or all punctuation marks.

/[\d\p{P}]+/  // Matches any string that contains a digit or punctuation mark

Character Classes

Character classes are used to match a range of characters, such as all lowercase letters or all digits.

/[0-9]/    // Matches any single digit
/[A-Za-z]/ // Matches any single uppercase or lowercase letter

The OR Operator

The | character is used as the OR operator in regular expressions, allowing you to match one of several alternatives.

/cat|dog/  // Matches either "cat" or "dog"

Flags

Flags are used to modify the behavior of a regular expression, such as making it case-insensitive or allowing it to match across multiple lines.

/hello/i  // Matches any string that contains the word "hello", regardless of case

Look-ahead and Look-behind

Look-ahead and look-behind assertions allow you to match a pattern only if it is followed or preceded by another pattern.

/\d+(?= dollars)/  // Matches a number only if it is followed by the word "dollars"
/(?<=\$)\d+/       // Matches a number only if it is preceded by a "$" sign

Author

Its Alfredo Tumax, a current fullstack bootcamp student. My GitHub:[https://github.com/yourvza]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment