/ posts

Understanding regex (for beginners)

🌱 This post is in the growth phase. It may still be useful as it grows up.

Regex is difficult to master but easy to get started with. Knowing a few basics will help a ton

Exact match

By default, regex will match the first instance of an exact pattern.

"Michael Chan".match(/Michael Chan/)
// Michael Chan

Capture group

Sometimes you don’t want to capture everything in a match. Use paretnthesis to capture set the boundaries of a capture group.

"Michael Chan".match(/Michael (Chan)/)
// Chan

Other examples to flesh out

"Michael Chan".match(/Michael (.+)/);
// => Chan
"Michael Chan".match(/Michael C(.+)n/);
// => ha