Diagram of the title regex

Jordan Kasper | @jakerella



String Pattern Matching

Often used for data validation

Test against pattern (true/false)

Find matching groups
(and possibly replace content)

What Language?


What Language?

What Engine?

Regex Engines

Same language, engines.

For example: PHP native versus PCRE

POSIX (standard), BRE/ERE (Linux (grep)), Perl, java.util.regex, XRegExp (JS)

What do they look like?

/the regex goes in here/
`the regex goes in here`
"the regex goes in here"
"/the regex goes in here/"

Okay, but what does it really look like?

preg_match("/foo/", "foobar");
/foo/ =~ "foobar"
"foobar" =~ /foo/

Simple Character Test

/a/.test("JavaScript Rules");  // true
/z/.test("JavaScript Rules");  // false

Testing versus Matching

true/false vs. matched groups

/a/.test("JavaScript Rules");   // true
"JavaScript Rules".match(/a/);  // ["a"]

"JavaScript Rules".match(/z/);  // null

Where's the other "a"?

"JavaScript Rules".match(/a/);  // ["a"]

Other Flags

"JavaScript Rules".match(/r/);  // ["r"]

// global
"JavaScript Rules".match(/r/g);  // ["r"]

Multiple Characters

Any Character (.)

Classes, Ranges, and Repetition

[ ], ?, +, *, { }



Escaping Characters

\ . ? * + - | [ ] ( ) { } ^ $

Grouping and Alternation

( | )

Group Matching Caution

Creating a matched group adds processing time!


This differs from engine to engine!

"I like {{fav}} the most".replace(/{{fav}}/, "dogs");

"I like dogs the most"

    .replace(/([a-z]+)@([a-z.]+)/i, "https://$2/users/$1");


Whitespace & Shorthands

Shorthand Example


^ and $


^ and $

Efficiency Warnings

Watch out for:

  • Too many groups (use non-matching)
  • Large source texts (use anchors)
  • Unstructured data (like HTML)

HTML Warning

HTML is not well structured, be careful!



Thank You!


Jordan Kasper | @jakerella


Bonus Content!

Look Arounds

They are not matches, but assertions.

Positive Look Ahead


The fat cat sat on the mat

Negative Look Ahead


The fat cat sat on the mat

Look Behind

Same idea, but looking backward instead of forward.

Not supported in all languages!
(notabley: JavaScript)

Look Behind


The fat cat sat on the mat

We also have negative look behind with: (?<!)