1 of 28

Regular Expressions

2 of 28

What is A Regular Expression?

Regular expressions describe a pattern of characters

  • Strings starting with "tech":

techtonica

technology

hackathon

talk

  • Strings composed of 1 or more numbers:

123

1949919

abc123

techtonica

3 of 28

What are regexes used for?

They’re used to find and/or replace words or character patterns inside strings.

  • Does my string contain anything that looks like a phone number?
  • Replace all instances of "JS" with "Javascript".

4 of 28

Regular Expressions Are Text!

techtonica

ab..

\d+

But! Regular expressions give some characters special meaning.

5 of 28

Characters

  • . matches anything!

.

1

a

8

z

6 of 28

Characters

  • \d matches digits.

\d

1

a

8

z

7 of 28

Characters

  • \w matches letters and numbers and underscore.

\w

A

a

8

%

8 of 28

Repetition

  • * means zero or more times.

a*

a

aaaaaa

9 of 28

Repetition

  • + means one or more times.

a+

a

aaaaaa

10 of 28

Repetition

  • ? means exactly 0 or 1 times.

a?

a

aaaaaa

11 of 28

Start and end of line

  • ^ means start of line.
  • $ means end of line.

^hello$

hello

hello world

12 of 28

Characters

  • . matches anything!

^ . . . $

aa3

a

812

abcd

13 of 28

Practice: which ones match the regex?

\d+

a

1

123

14 of 28

\d+

a

1

123

Practice: which ones match the regex?

15 of 28

Practice: which ones match the regex?

.*

a

1

123abc

16 of 28

Practice: which ones match the regex?

.*

a

1

123abc

17 of 28

Using Regex in Javascript

  • Regular expressions are built into Javascript (not a library, but part of the language).

// define a regular string:

let x = "mystring";

// regexes use / instead of "

let regex = /techtonica/

18 of 28

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.

19 of 28

RegEx Syntax for a Basic Pattern

  • The pattern you’re looking for goes between the forward slashes

let regexToSearchFor = /beach/;

Pattern

20 of 28

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

21 of 28

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

22 of 28

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!”

23 of 28

RegEx Syntax for Modifiers

  • Modifiers go after the final forward slash ( / ). They modify the search.
  • Modifiers are not required, but commonly used
  • The letter “g” will return (or replace) all occurrences of the search, not just the first occurrence
  • The letter “i” will return all occurrences of the pattern regardless of capitalization

var regexToSearchFor = /beach/gi;

Modifiers

24 of 28

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”

25 of 28

RegEx Syntax - More Searches

  • Brackets
    • [asdfjkl] Find any characters inside the brackets
    • [^asdfjkl] Find any characters not inside the brackets
    • [g-s] Find any characters in the range inside the brackets
    • [^g-s] Find any characters not in the range inside the brackets
    • Works for numbers, too!

26 of 28

Regular Expressions (RegEx)

Lots more syntax for doing cool things!

Click here

27 of 28

Regular Expressions (RegEx)

Why might RegEx be useful to software engineers?

28 of 28

Regular Expressions (RegEx) Resources