Fireship · @fireship

Regular Expressions (RegEx) in 100 Seconds

Uploaded May 19, 2020 · Length 2:22

Regular Expressions (RegEx) in 100 Seconds

Summary

learn regex patterns for text validation and search in programming languages like JavaScript

  • regex
  • regular expressions
  • text search
  • pattern matching
  • javascript

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Regular Expressions, aka regex or regex if you prefer, is a tool for finding patterns

  2. 0:05

    within a string.

  3. 0:06

    Its two most common use cases include the validation of user-generated input and to

  4. 0:11

    search through a large body of text, such as a find and replace operation in a document.

  5. 0:15

    It's notoriously difficult to learn because its syntax ends up looking like a meaningless

  6. 0:19

    pile of gibberish.

  7. 0:20

    But once you learn it, you'll have a very powerful and universal way to parse text

  8. 0:26

    in any programming language.

  9. 0:27

    For example, in JavaScript, you can use match to find all the matching patterns within a

  10. 0:31

    string, search to test for a single match, and replace to replace the matching patterns.

  11. 0:36

    But before you can put these to use, you need to know how to write a regular expression.

  12. 0:39

    In JS, we can do that with the literal syntax by starting with two forward slashes.

  13. 0:44

    Between these characters is where we define our search pattern.

  14. 0:46

    You can find an exact match by simply typing the string you're looking for.

  15. 0:49

    By default, it will only find the first match.

  16. 0:51

    However, you can change this behavior by adding a flag to the right side of the closing slash.

  17. 0:56

    the G flag means global, and we'll look for multiple matches in the text.

  18. 0:59

    And you can add additional flags here as well.

  19. 1:01

    Now in order to create a more complex search pattern, you can bind a handful of special

  20. 1:05

    characters within your expression Use a vertical bar as a logical OR To match one string or the other you can group your logic using parentheses This isolates your code from the rest of the pattern so you can capture substrings You can use quantifiers to control how often the preceding

  21. 1:19

    pattern is allowed to occur. A question mark allows a pattern to occur zero or one times.

  22. 1:24

    An asterisk allows a pattern to occur zero or multiple times. And a plus sign means one or more

  23. 1:29

    times. Or you can control this behavior explicitly using braces, setting an exact number of occurrences

  24. 1:34

    or a min max value. You can use a backslash in your pattern to escape any of these special

  25. 1:38

    characters. Or a backslash might refer to a character class, like D for any digit, or W

  26. 1:43

    for any word character. Make these a capital letter, and now you have anything that's not a

  27. 1:47

    digit or not a word character. You can use brackets to define your own custom character

  28. 1:51

    sets, like a range of all capital letters from A to Z, or a custom character set that you create.

  29. 1:56

    And you can negate the set by adding a caret in front of it. But like I said at the beginning,