Advertisement
Guest User

Untitled

a guest
May 6th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. Regular Expressions
  2.  
  3. What is a regular expression?
  4.  
  5. Let's begin by actually opening up our console – go to any page and pop open your inspector. (command + option + I)
  6.  
  7. A regular expression is a form of notation used to identify patterns within strings.
  8. They are most notably used for string validations: for example verifying that a user's input into a form field is actually an email address based on a particular pattern.
  9.  
  10. We've actually seen a little taste of regex before, can anyone remember?
  11.  
  12. A regular expression for verifying a user has input a first and last name into a form field might look something like this:
  13. /^([A-z][A-Za-z]*\s+[A-Za-z]*)|([A-z][A-Za-z]*)$/
  14.  
  15.  
  16.  
  17. This pretty much looks like the Rosetta Stone right now, but we will break it down into smaller pieces.
  18.  
  19. Regular expressions, also commonly called RegEx originated in the 1950s and are widely supported today in most programming languages (Java, Python, Ruby, C+ +) and native to most browsers. Obviously, today we will be talking about how to incorporate them into our JavaScript code.
  20.  
  21. So how do we use regular expressions?
  22.  
  23. Now, There are two ways to create a Regular Expression: we can create a constructor
  24.  
  25. >>> RegExp(“abc”);
  26.  
  27. which is useful when we know the pattern is changing or need to capture information in a variable
  28.  
  29. >>var foo = “123”
  30.  
  31. >> RegExp(“abc” + foo”)
  32.  
  33.  
  34. We can use the literal notation by entering the pattern between two front slashes when we know the expression will remain constant.
  35.  
  36. Let's write a literal expression of the word hello.
  37.  
  38. >> /hello/
  39.  
  40. What we have created here is a regular expression that matches strings that contain the string “hello” together in this order. We would use an expression like this when we want to find an exact match within a string.
  41.  
  42. For example, for this expression we would find a match in a string that says, “hello, front-end!” but not in a string like this “he llo” because it is not the exact pattern of h-e-l-l-o. We have that space.
  43.  
  44. I am going to use a String method that executes a search for a match in a string. It will return an array of information or it will be null.
  45.  
  46. >> "hello".match(/hello/) finds the matching pattern
  47.  
  48.  
  49. >> console.log("he llo, front-end".match(/hello/)); womp womp
  50.  
  51. What if our situation was a little more complicated than finding an exact match?
  52. Well, regex provides us with a list special characters that can enhance our matching abilities.
  53.  
  54. Some of the special characters we have in our regex tool belt are: ^ $ | * [] and we will go over some of their use cases. There is a nice list on MDN that I recommend bookmarking for reference as well.
  55.  
  56. We can use these characters for situations in which we might need to find a string with white space, or a reoccurring letter, etc.
  57.  
  58. \d represents a character from 0-9
  59.  
  60. >> "I have 2 eyes".match(/\d/)
  61.  
  62. you can add on character/s to the special character as well
  63.  
  64. >> "I am the 3rd child".match(/\drd/)
  65.  
  66. \s represents a whitespace character
  67.  
  68. >> "I am the 3rd child".match(/\drd\s/)
  69.  
  70. you can see the white space here added to our string in the console and how we might begin to add on more characters to create more intricate patterns.
  71.  
  72. \w represents a symbol of the latin alphabet or a digit or an underscore
  73. Any ideas of the expression we might write out to print “3rd Child?”
  74.  
  75. >> "I am the 3rd child".match(/\drd\s\w\w\w\w\w/)
  76.  
  77. “.” represents any character except a newline:
  78.  
  79. >> "tacos!".match(/tacos./) works!
  80. >> "tacos?".match(/tacos./) works!
  81.  
  82. >> "tacos".match(/tacos./) womp womp
  83.  
  84. Now knowing dot is a special character, but what if we want to match a pattern like “1.25”? How do we indicate this in our regex without causing confusion?
  85. We call this escaping.
  86.  
  87. To use one of our special characters as a regular symbol, it must be escaped.
  88. We escape by prepending with a blackslash.
  89.  
  90. How might we match 1.25 in this string?
  91.  
  92. >> "version 1.25".match(/\d\.\d\d/)
  93.  
  94. Now, what if our version name was even longer? 1.25356
  95. we could write this as:
  96.  
  97. >> "version 1.25356".match(/\d\.\d\d\d\d\d/)
  98.  
  99. Or we can use another special character – our curly braces to notate the digits in a number. Our version 1.25356 could be written as.
  100.  
  101. "version 1.25356".match(/\d\.\d{5}/)
  102.  
  103. Another important character is the asterisk.
  104. In the example /ab*c/
  105. “*” matches any character combination in which a single A is followed by zero or more Bs and then immediately followed by a c.
  106.  
  107. "ac".match(/ab*c/) = “ac”
  108. "cbbabbbbcdebc".match(/ab*c/) = "abbbbc"
  109.  
  110. Character sets are also an important aspect of pattern matching.
  111. [a-z] matches a lowercase character from a-z.
  112. "LIZ".match(/[a-z]/) = null
  113. "liz".match(/[a-z]/) = 'l' because it finds the first
  114.  
  115. on the contrary [A-Z] matches the uppercase
  116.  
  117. We've tackled a few of the most commonly used RegEx characters and their basic functionality. I highly recommend popping up MDN as you write so that you have a guide as you write your code.
  118.  
  119. Taking a look at our form field input from the beginning we can already identify a few key pieces we've learned here in just a few minutes:
  120.  
  121. /^([A-z][A-Za-z]*\s+[A-Za-z]*)|([A-z][A-Za-z]*)$/
  122.  
  123. The key to breaking down regex is breaking down your expressions into one little bite at a time. Don't be intimidated by their look!
  124.  
  125. Any questions?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement