Skip to content

JavaScript Regular Expressions / lesson 2 of 11

Plain text

Matching plain text with regular expressions is simpler than you think.

Play

Matching plain text with regular expressions is simpler than you think. This lesson covers the building blocks: matching literal text, using the global flag to find all occurrences, and the dot (.) meta character for matching any single character. We also cover the dot-all flag (s flag) that lets the dot match line breaks, and how to escape meta characters when you need to match them literally.

app.js

app.ts
import output from "./output.js";
// . [ ] ( ) { } * + ? ^ $ | \ /
// finding a plain text patters with regexp is fairly trivial
const str = `Cat sat on the mat.`;
// Match literal text
let regex = /Cat/; // easy
// regex = /at/g; // all the at add g
// regex = /.at/g; // all the .at
// metacharacter dot identifies any character letters,
// digits, dashes, but not line breaks
// regex = /./g; // all the .at
// regex = /.../gi; // any set of 3 chars
// regex = /..../gi; // any set of 4 chars, no line breaks
// regex = /..../gis; // include line breaks with dotAll
// regex = /\./; // literal .
output(str, regex);

Share this post on:

Previous
Introduction
Next
Quantifiers