Ripgrep: How to Match a Whole Word

Updated

Learn how to match a whole word only with ripgrep (rg)

Ripgrep: How to Match a Whole Word

Before we start, make sure you have ripgrep installed on your computer. On a Mac, you can install it with Homebrew:

brew install ripgrep

First, open your terminal and navigate to your project folder. To match a whole word only, add the -w flag:

rg -w "word"

For example, rg -w "log" finds console.log but skips login, logout and loggedIn. A dot or a bracket counts as a word boundary, so console.log still matches.

You can match several whole words at once, too. -w wraps the whole pattern in word boundaries, so this finds foo or bar but not foobar:

rg -w "foo|bar"

The results come back right away, and you can compare the count with a search without -w to see what was filtered out.

undefined

You can also put the word boundaries in the pattern itself with \b, like rg '\blog\b'. Newer versions of ripgrep also support \< and \>, so rg '\' works too.