Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A Regular Expression lets you match patterns in strings. We'll start with a simple example, using the Chrome browser's find functionality.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hi, I'm Joel, a JavaScript developer and
teacher at Treehouse.
0:04
Welcome to Regular Expressions
in JavaScript.
0:09
Have you ever wondered how search
bars find the text you type in?
0:13
Or maybe when you are filling out a form,
0:16
it told you had entered
an invalid email address.
0:18
How did it know?
0:22
Regular expressions is the answer.
0:24
In this course, I'll show you
what regular expressions are.
0:27
And we'll get some practice using them.
0:30
Then we'll build a form that uses regular
expressions to validate the data typed in.
0:32
What is a regular expression?
0:39
A regular expression is a way to
describe a pattern in a string.
0:41
Let's look at a simple example
using the browser's find feature.
0:46
I'm on the Wikipedia page for
regular expressions.
0:51
It says here, Stephen Cole Kleene helped
found the concept of regular expressions.
0:55
I'll find his name on the page.
1:00
I'll hit Cmd+F to open Chrome's find bar.
1:02
As I type in Kleene's name,
1:07
notice that Chrome highlights
each place the word appears.
1:10
These highlighted strings
are called matches
1:14
because they match
the string I'm typing in.
1:17
The matches are case insensitive.
1:21
In other words, even though I didn't type
a K, Chrome still matches the word Kleene.
1:23
If I then type the space,
this Kleene is no longer highlighted
1:31
because it doesn't match
what I'm searching for.
1:36
If I backspace and type an apostrophe,
this one deselects.
1:39
What happens if I want to
select both of these at once?
1:45
You can do that using special
characters in a regular expression.
1:49
Because Chrome's simple search bar
doesn't recognize regular expressions,
1:53
let's use an online
editor called RegexPal.
1:58
The version I'm using has a long URL, so
2:01
the best way to get to RegexPal is by
clicking a link in the teacher's notes.
2:04
Regex is a common way to shorten
the term regular expression.
2:09
On this page, we can test regexes and
interactively learn how they work.
2:13
Type the text you want to
match into the lower window.
2:18
This is kind of like the Wikipedia page,
I'll type Kleene's name.
2:22
In the upper field, I'll type Kleene.
2:28
The top text field is like
the find bar of the browser.
2:31
In the bottom field,
Kleene highlights, it's a match.
2:35
You type the regular expression
into the top field and
2:38
any text you want to search
into the bottom field.
2:41
In the bottom field,
I'll type Kleene again.
2:44
And you can see that both
instances are matched.
2:48
Let's replicate the search we did
earlier on the Wikipedia page.
2:51
I'll put a space after one and
an apostrophe after another.
2:55
Now when I type a space in the upper
field, we see a similar behavior.
3:01
Only one of them is highlighted.
3:06
When I type an apostrophe,
instead, the other is highlighted.
3:08
So far, I've just been putting
strings into the top field.
3:12
A string as a kind of regex that
only matches exact duplicates.
3:16
But we can use other special characters to
create a more flexible regular expression.
3:21
For example, I'll add a dot or
period to the end of this.
3:27
And you can see that
both strings are matched.
3:31
A dot has a special meaning
in regular expression.
3:34
It matches any single character, so
it will match both the space and
3:38
the apostrophe.
3:42
I discuss this and other special regex
characters in more detail later.
3:44
So don't worry too much about it for now.
3:49
I just wanted to give you
a glimpse of what's possible.
3:51
In the next video, I'll teach you
more about special regex characters.
3:54
And how you can use them
to find patterns in text.
3:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up