Regular Expressions
What is A Regular Expression?
Regular expressions describe a pattern of characters
techtonica
technology
hackathon
talk
123
1949919
abc123
techtonica
What are regexes used for?
They’re used to find and/or replace words or character patterns inside strings.
Regular Expressions Are Text!
techtonica
ab..
\d+
But! Regular expressions give some characters special meaning.
Characters
.
1
a
8
z
Characters
\d
1
a
8
z
Characters
\w
A
a
8
%
Repetition
a*
a
aaaaaa
Repetition
a+
a
aaaaaa
Repetition
a?
a
aaaaaa
Start and end of line
^hello$
hello
hello world
Characters
^ . . . $
aa3
a
812
abcd
Practice: which ones match the regex?
\d+
a
1
123
\d+
a
1
123
Practice: which ones match the regex?
Practice: which ones match the regex?
.*
a
1
123abc
Practice: which ones match the regex?
.*
a
1
123abc
Using Regex in Javascript
// define a regular string:
let x = "mystring";
// regexes use / instead of "
let regex = /techtonica/
6 Ways to Use Regex in Javascript
// Basic methods
regex.test(string) // returns true if `string` matches the regex.
string.replace(regex, replacement) // replace instances of the regex with replacement.
// Advanced methods
string.match(regex) // returns the matches between string and regex.
regex.exec(string) // returns the matches between string and regex.
string.search(regex) // returns the index of the first match of regex within string.
string.split(regex) // splits string at every instance where regex matches.
RegEx Syntax for a Basic Pattern
let regexToSearchFor = /beach/;
Pattern
RegEx Syntax - Testing A String for A Pattern
let myFunStr = “I love the beach!”;
let regexToSearchFor = /beach/;
regexToSearchFor.test(myFunStr); // returns true
let anotherStr = “I love the ocean!”;
regexToSearchFor.test(anotherStr); // returns false
RegEx Syntax - Testing A String for A Pattern
let str1 = "I ate 123 ice cream flavors";
let regexToSearchFor = /\d+/;
regexToSearchFor.test(str1); // returns true
let str2 = "I ate four ice cream flavors";
regexToSearchFor.test(str2); // returns false
RegEx Syntax - Replacing A Pattern In A String
// Code we already have
let myFunStr = “I love the beach!”;
let regexToSearchFor = /beach/;
// New code
let newFavePlace = “forest”;
let newMyFunStr = myFunStr.replace(regexToSearchFor, newFavePlace);
console.log(newMyFunStr); // “I love the forest!”
RegEx Syntax for Modifiers
var regexToSearchFor = /beach/gi;
Modifiers
RegEx Syntax - Replacing A Pattern In A String
// Code we already have
let myFunStr = "I love the beach! BEACHES ARE GREAT";
let regexToSearchFor = /beach/gi;
// New code
let newFavePlace = "forest";
let newMyFunStr = myFunStr.replace(regexToSearchFor, newFavePlace);
console.log(newMyFunStr); // “I love the forest! forestES ARE GREAT”
RegEx Syntax - More Searches
Regular Expressions (RegEx)
Lots more syntax for doing cool things!
Regular Expressions (RegEx)
Why might RegEx be useful to software engineers?
Regular Expressions (RegEx) Resources
RegEx in JS - W3Schools
The Regular Expression Object in JS - W3Schools
Interactive Regex Exercise - Regex One
Interactive Regex Visualizer - Regexr