, 12 tweets, 4 min read
🔥 Intro to regex for web developers 👇
1/10

Regular expressions find parts of a string that match a pattern

In JS they're created in between forward slashes //, or with `new RegExp()`

and then used in methods like `match`, `test`, or `replace`

You can define the regex beforehand or directly when calling the method
2/10

Match individual characters one at a time,

or put multiple characters in square brackets [] to capture any that match

Capture a range of characters with a hyphen -
3/10

Add optional flags to the end of a regex to modify how the matcher works.

In JavaScript, these flags are:

i = case insensitive
m = multi line matching
g = global match (find all, instead of find one)
4/10

Using a caret ^ at the start means "start of string"

Using a dollar sign $ at the end means "end of string"

Start putting groups of matches together to match longer strings
5/10

Use wildcards and special escaped characters to match larger classes of characters

. = any character except line break

\d = digit
\D = NOT a digit

\s = white space
\S = any NON white space

\n new line
6/10

Match only certain counts of matched characters or groups with _quantifiers_

* = zero or more
+ = one more more
? = 0 or 1
{3} = exactly 3 times
{2, 4} = two, three, or four times
{2,} = two or more times
7/10

Use parens () to capture in a group

`match` will return the full match plus the groups, unless you use the `g` flag

Use the pipe operator | inside of parens () to specify what that group matches

| = or
8/10

To match special characters, escape them with a backslash \

Special characters in JS regex are: ^ $ \ . * + ? ( ) [ ] { } |

So to match an asterisks, you'd use:

\*

Instead of just *
9/10

To match anything BUT a certain character, use a caret ^ inside of square brackets

This means ^ has two meanings, which can be confusing.

It means both "start of string" when it is at the front of a regex, and "not this character" when used inside of square brackets.
10/10

Regexs can find and match all sort of things, from urls to filenames

HOWEVER! be careful with complex tasks, such as parsing email addresses, or HTML (which is not a regular language, and so can't be fully parsed by a regular expression)
There is (of course) much more to regex like lazy vs greedy, lookahead, and capturing

but most of what web developers want to do with regular expressions can use just these base building blocks.

I'm already writing a follow up post with a bunch of real world regex use cases 🎉
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Chris Achard
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!