avatarSolomon Richards

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1744

Abstract

</span> timRegex = <span class="hljs-regexp">/Tim{4}ber/</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = timRegex.<span class="hljs-title function_">test</span>(timStr); </pre></div><h2 id="4b18">Checking Existence</h2><p id="7d93">Using the question mark operator, I can check for elements that may or may not exist in an expression. So in the English language, there are inconsistencies in spelling. Sometimes, the word ‘favorite’ can be spelled with ‘o’, and sometimes it is spelled with ‘ou’. Here is a code example of what I am talking about:</p><div id="3f44"><pre><span class="hljs-keyword">let</span> favWord = <span class="hljs-string">"favorite"</span>; <span class="hljs-keyword">let</span> favRegex = <span class="hljs-regexp">/favou?rite/</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = favRegex.<span class="hljs-title function_">test</span>(favWord);</pre></div><h2 id="80aa">Positive and Negative Lookaheads</h2><p id="3973">I will use lookaheads to check for patterns in a string that come down the line. They said it would be good to check for different patterns over strings. There are two types of lookaheads: <i>positive and negative lookaheads</i>. They are both slightly different so let’s get into it.</p><div id="6ec6"><pre><span class="hljs-comment">// Positive Lookahead</span> <span class="hljs-keyword">let</span> sampleWord = <span class="hljs-string">"astronaut"</span>; <span class="hljs-keyword">let</span> pwRegex = <span class="hljs-regexp">/(?=\w{6})(?=\D*\d{2})/</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = pwRegex.<span

Options

class="hljs-title function_">test</span>(sampleWord);

<span class="hljs-comment">// Mixed Character Grouping</span> <span class="hljs-keyword">let</span> myString = <span class="hljs-string">"Eleanor Roosevelt"</span>; <span class="hljs-keyword">let</span> myRegex = <span class="hljs-regexp">/(Eleanor|Franklin) (([A-Z].?|[A-Z][a-z]+) )?Roosevelt/</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = myRegex.<span class="hljs-title function_">test</span>(myString); <span class="hljs-comment">// Change this line</span>

<span class="hljs-comment">// Capture Groups</span> <span class="hljs-keyword">let</span> repeatNum = <span class="hljs-string">"42 42 42"</span>; <span class="hljs-keyword">let</span> reRegex = <span class="hljs-regexp">/^(\d+) \1 \1$/</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = reRegex.<span class="hljs-title function_">test</span>(repeatNum);

<span class="hljs-comment">// Last Problem</span> <span class="hljs-keyword">let</span> hello = <span class="hljs-string">" Hello, World! "</span>; <span class="hljs-keyword">let</span> wsRegex = <span class="hljs-regexp">/^\s+|\s+$/gi</span>; <span class="hljs-comment">// Change this line</span> <span class="hljs-keyword">let</span> result = hello.<span class="hljs-title function_">replace</span>(wsRegex, <span class="hljs-string">""</span>); </pre></div><p id="bee5">That was the final problem in the regex. At long last, I can move on from this tiresome section. I hope you all enjoyed the regular expressions part of this vlog. The upcoming section will be about debugging. That should be fun. Keep reading for that.</p></article></body>

Ending Regular Expressions: Learning JavaScript

Today, I will finish the regular expressions section of the JavaScript BootCamp I have been doing for the past month. This section of the course was pretty tricky. I pulled through in the end though. So here are the last bits of code I learned.

Quantity Specifiers

In this section, I will use the quantity specifiers to specify upper and lower matches in patterns. That involves curly braces. I put two numbers in the curly braces. That is the number of times a character can match during an expression.

let ohStr "Ohhh no";
let ohRegex = /Oh{3,6} no/; // Change this line
let result = ohRegex.test(ohStr);

// Lower Number of Matches
let haStr = "Hazzzzah";
let haRegex = /Haz{4,}ah/; // Change this line
let result = haRegex.test(haStr);

// Exact Number of Matches
let timStr = "Timmmmber";
let timRegex = /Tim{4}ber/; // Change this line
let result = timRegex.test(timStr);

Checking Existence

Using the question mark operator, I can check for elements that may or may not exist in an expression. So in the English language, there are inconsistencies in spelling. Sometimes, the word ‘favorite’ can be spelled with ‘o’, and sometimes it is spelled with ‘ou’. Here is a code example of what I am talking about:

let favWord = "favorite";
let favRegex = /favou?rite/; // Change this line
let result = favRegex.test(favWord);

Positive and Negative Lookaheads

I will use lookaheads to check for patterns in a string that come down the line. They said it would be good to check for different patterns over strings. There are two types of lookaheads: positive and negative lookaheads. They are both slightly different so let’s get into it.

// Positive Lookahead
let sampleWord = "astronaut";
let pwRegex = /(?=\w{6})(?=\D*\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);

// Mixed Character Grouping
let myString = "Eleanor Roosevelt";
let myRegex = /(Eleanor|Franklin) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/; // Change this line
let result = myRegex.test(myString); // Change this line

// Capture Groups
let repeatNum = "42 42 42";
let reRegex = /^(\d+) \1 \1$/; // Change this line
let result = reRegex.test(repeatNum);

// Last Problem
let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/gi; // Change this line
let result = hello.replace(wsRegex, ""); 

That was the final problem in the regex. At long last, I can move on from this tiresome section. I hope you all enjoyed the regular expressions part of this vlog. The upcoming section will be about debugging. That should be fun. Keep reading for that.

JavaScript
Javascript Development
Coding
Learning
Technology
Recommended from ReadMedium