• code

    Regular Expressions Extract Matches

    So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the .match() method. To use the .match() method, apply the method on a string and pass in the regex inside the parentheses. Here’s an example: Here the first match would return ["Hello"] and the second would return ["expressions"]. Note that the .match syntax is the “opposite” of the .test method you have been using thus far:

  • code

    Regular Expressions Cheat Sheet

    Operator Code Example Or | /yes|no/let testStr = "Pumpkin"; let testRegex = /P(engu|umpk)in/; testRegex.test(testStr); Ignore Case i /Matt/i Extract Multiple Instances g /Repeat/g Wildcard (one character) . /hu./ Character Classes [ ] /b[aiu]g/ Range [-] /[a-e]at//[a-z0-9]/ Avoid Match [^] /[^aeiou]/ One or More Matches + /a+/ Zero or More Matches * /go*/ Lazy Match (smallest result) ? /<.*?>/ Beginning of String ^ /^Ricky/ Ending of String $ /story$/ Match All Numbers/Letters/Underscore \w /\w+//[A-Za-z0-9_]+/ ~Match All Numbers/Letters/Underscore \W Match All Numbers \d Match All Non-Numbers \D Match Whitespace \s Match Non-Whitespace \S Specify Number of Matches {#,#} a{2,3}ha{2,}ha{2}h Optional Match ? /colou?r/ Positive Lookahead (?=...) let quRegex = /q(?=u)/; Negative Lookahead…

  • code

    Regular Expressions Test Method

    Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching. If you want to find the word the in the string The dog chased the cat, you could use the following regular expression: /the/. Notice that quote marks are not required within the regular expression. JavaScript has multiple ways to use regexes. One way to test a regex is using the .test() method. The .test() method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not. The test method here returns true. In the last challenge, you searched for the word Hello using the regular expression /Hello/. That regex searched for…

  • code

    JavaScript Promises

    A promise in JavaScript is exactly what it sounds like – you use it to make a promise to do something, usually asynchronously. When the task completes, you either fulfill your promise or fail to do so. Promise is a constructor function, so you need to use the new keyword to create one. It takes a function, as its argument, with two parameters – resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: Complete a Promise with resolve and reject A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did not add a way to…

  • code

    JavaScript Import Code Blocks

    In the last challenge, you learned about export default and its uses. To import a default export, you need to use a different import syntax. In the following example, add is the default export of the math_functions.js file. Here is how to import it: The syntax differs in one key place. The imported value, add, is not surrounded by curly braces ({}). add here is simply a variable name for whatever the default export of the math_functions.js file is. You can use any name here when importing a default.

  • code

    JavaScript Export Code Blocks

    Create an Export Fallback with export default In the export lesson, you learned about the syntax referred to as a named export. This allowed you to make multiple functions and variables available for use in other files. There is another export syntax you need to know, known as export default. Usually you will use this syntax if only one value is being exported from a file. It is also used to create a fallback value for a file or module. Below are examples using export default: The first is a named function, and the second is an anonymous function. Since export default is used to declare a fallback value for a module or file, you can only have one…

  • code

    JavaScript Modules

    Create a Module Script JavaScript started with a small role to play on an otherwise mostly HTML web. Today, it’s huge, and some websites are built almost entirely with JavaScript. In order to make JavaScript more modular, clean, and maintainable; ES6 introduced a way to easily share code among JavaScript files. This involves exporting parts of a file for use in one or more other files, and importing the parts you need, where you need them. In order to take advantage of this functionality, you need to create a script in your HTML document with a type of module. Here’s an example: A script that uses this module type can now use the import and export features you will…

  • code

    JavaScript EC6 Object Features

    Use Destructuring Assignment to Extract Values from Objects Destructuring assignment is special syntax introduced in ES6, for neatly assigning values taken directly from an object. So, it’s kinda turned around. Again, name would have a value of the string John Doe, and age would have the number 34. Here, the name and age variables will be created and assigned the values of their respective values from the user object. You can see how much cleaner this is. You can extract as many or few values from the object as you want. New variable names. You may read it as “get the value of user.name and assign it to a new variable named userName” and so on. The value of userName would be the string John Doe, and the value…

  • code

    JavaScript Arrow Functions

    Use Arrow Functions to Write Concise Anonymous Functions In JavaScript, we often don’t need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don’t need to name these functions because we do not reuse them anywhere else. To achieve this, we often use the following syntax: ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax: When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword return as well as the brackets surrounding the code. This helps simplify…