Learning JavaScript
Regular Expressions
Creating Regular Expressions
//2 ways to create regular Expressions
//using RegExp() constructor
const value = new RegExp("expression"[,"flags"]);
//regular expression literal
const value = /expression/[flags];
Creating Regular Expressions
//2 ways to create regular Expressions
//using RegExp() constructor
const value = new RegExp("expression"[,"flags"]);
//ex
const pattern = new RegExp("Lincoln");
//regular expression literal
const value = /expression/[flags];
//ex
const pattern = /Lincoln/;
Testing Regular Expression Pattern
//strings to test
const president = "Abraham Lincoln";
const vicePresident = "Andrew Johnson";
//using test method
pattern.test(president); //true
pattern.test(vicePresident); //false
Case Sensitivity
//case insensitive
//use "i"
//RegEx constructor
const pattern = new RegExp("lincoln", "i");
//expression literal
const pattern = /lincoln/i;
//strings to test
const president = "abraham lincoln";
const vicePresident = "Andrew Johnson";
const presidentLastFirst = "Lincoln, Abraham";
//using test method
pattern.test(president); //true
pattern.test(vicePresident); //false
pattern.test(presidentLastFirst); //true
Special Characters
Pattern Matches
\\ Backslash character
\/ Forward Slash
\t tab
\n newline
\r carriage return
\f form feed
\v vertical tab
[\b] backspace [brackets mandatory]
Types of Characters
Pattern Matches
. any character except a newline(use \. for a period)
[ ] any character in brackets (use \[ \] to match bracket symbol)
[^ ] any character not in brackets
[a-z] any character in range of characters inside brackets
\w any letter, number or underscore
\W any character that isn't letter, number or underscore
\d any digit
\D any character that isn't a digit
\s any whitespace character
\S any character that's not a whitespace
Example Special Character Match
//looking for "/"
let str = "2020 Company Etc.\nMilford, Georgia [09/2022]";
let pattern = /\//;
pattern.test(str); //true
Example Charcter Searches
//looking for four digits
const pattern = /\d{4}/;
let str = "55-5555"
pattern.test(str); //true
//look for cerain characters
let productCode = "XYZ-458-22";
let pattern = /XY/;
pattern.test(productCode); //true
productCode = "HHW-390-32"
pattern.test(productCode); //false
//look for any one character from inside bracket
let productCode = "XYZ-458-22";
let pattern = /XY[ZAB]/;
pattern.test(productCode); //true
String Positions
Pattern Matches
^ beginning of string (use \^ to match caret)
$ end of string (use \$ to match $)
\b word character now followed or preceded by a word character
\B word character followed by or preceded by a word character
Search for RegEx pattern at beginning of string
//pattern
const pattern = /^Lincoln/;
//strings to test
const president = "Abraham Lincoln";
const vicePresident = "Andrew Johnson";
const presidentLastFirst = "Lincoln, Abraham";
//using test method
pattern.test(president); //false
pattern.test(vicePresident); //false
pattern.test(presidentLastFirst); //true
Search for RegEx pattern at end of string
//pattern
//use $ at end of string /...$/
const pattern = /Lincoln$/;
//strings to test
const president = "Abraham Lincoln";
const vicePresident = "Andrew Johnson";
const presidentLastFirst = "Lincoln, Abraham";
//using test method
pattern.test(president); //true
pattern.test(vicePresident); //false
pattern.test(presidentLastFirst); //false
Repeating Patterns
Pattern Matches
{n} where n is number desired (use \{\} to match brace)
{n,} pattern must repeat n or more times
{n,m} subpattern must repeat from n to m times
? zero or one of the previous subpattern
+ one or more of previous subpattern
* zero or more of previous subpattern
Your typical phone number test
//pattern
let pattern = /^d{3}-\d{3}-\d{4}$/;
//strings to test
let str1 = "555-556-5566";
let str2 = "(555) 555-4444";
//using test method
pattern.test(str1); //true
pattern.test(str2); //false
pattern = /^(\d{3}\) ?\d{3}-\d{4}$/ //? means pattern will match with space or not
pattern.test(str2); //true
let str3 = "(555)554-1234";
pattern.test(str3); //true
//match phone number with or without ()
let pattern = /^(\d{3}-) | (\(\d{3}\) ?)\d{3}-\d{4}$/; //"|" means or in RegEx
pattern.test(str1); //true
pattern.test(str2); //true