Using Regex with WP CLI to Search & Replace in the Database

Quick one this. WP CLI documentation on executing a search and replace on the database using regex can be confusing. Here’s a one liner that works and can eliminate some confusion. Feel free to tweak and play (on a sandbox install).

For this to run, you’ll need a WordPress install and access to WP CLI. Do not forget to replace the regex with your own.

This is the first thing you should try before trying something crude like replacing via PHP.

wp search-replace '<script.*?217bc9f0d28b2da070\.js.*?<\/script>' '' --all-tables --dry-run --report-changed-only --precise --regex --regex-delimiter='/'

Leave a Reply

Your email address will not be published. Required fields are marked *