Free

Free coded apps to suit all. No sign up, no email, usable offline, updates when I can, feedback welcome. Send feedback

How-to

Regex find and replace in a code editor

Use regular expressions in FTP by Zaweba's Replace bar to change patterns, not just exact words, across a file you are editing on your website.

Regex find and replace in a code editor

FTP by Zaweba has regular expression find and replace built into its code editor. Open the Replace bar, switch on Regular expression, type a pattern in the find box and your replacement in the other, then use Replace to change one match at a time or Replace All to change every match in the open file.

That is the short answer. The rest of this article is about using it without breaking a page, because a pattern that matches more than you meant can do a lot of damage in one click.

Why a pattern beats a plain word

A plain search finds one exact piece of text. A regular expression describes a shape of text: any number, any run of spaces, a word at the start of a line, a tag with anything inside it. That matters on websites, where the same kind of thing turns up again and again with small differences. Think of image paths that all start the same way but end in different file names, years in a copyright line, or class names that share a prefix.

In FTP by Zaweba the editor is where you already work on your site's PHP, HTML, CSS and JavaScript, so you don't need to copy code out to another tool, change it and paste it back. The file stays in its tab, with its syntax colours, and you can save it straight away or let Upload on Save send it to the server.

Step by step

Open the file from the local tree. Bring up Find and Replace and look at the three switches beside the boxes: Match case, Whole word and Regular expression. Turn on Regular expression. Leave Match case on if capital letters matter in what you are searching for, which in code they usually do.

Type your pattern and start with Find Next (F3) rather than a replace button. Each press moves to the next match, and Find Previous (Shift+F3) moves back. Step through a handful of matches and check each is something you really want to change. This is the cheapest test you will ever run.

When the matches look right, type the replacement. Use Replace on the first one and read the result in place. If it is what you wanted, carry on with Replace for each match, or press Replace All once you trust the pattern.

Go to Line is in the same family of tools. If a preview console message or a colleague points you at line 212, jump there directly instead of scrolling.

Getting a good result

Keep patterns narrow. A dot in a regular expression means any character, so a pattern meant to find a file name with a full stop in it can also catch text you never expected. Escaping that dot, or anchoring the pattern to something distinctive nearby, keeps it honest.

Watch for greedy matches in HTML. A pattern that runs from an opening angle bracket to a closing one can swallow half a line when several tags sit side by side. If Find Next highlights more than you expected, tighten the pattern before you replace anything.

Save before a big Replace All. The editor has Undo, and FTP by Zaweba also keeps a local version history: before an editor save overwrites a local file, the previous version is kept on your PC, with Compare with current and Restore in the history window. Between the two, a bad replace is recoverable, but it is easier not to need them.

Check the page afterwards. Live Preview (Ctrl+Shift+P) shows the page beside the code, and HTML updates as you type, so a broken tag shows up at once.

When one file is not enough

The Replace bar works on the open file. If the same pattern lives in many files, Find in Files searches across the whole site with the same options as Find, including regular expressions, and lists every hit in the bottom panel. That is a good way to see the size of a job before you start it, then open each file and replace with care. A long search can be cancelled with Stop.

Other ways to do it

You could do the same job in a separate text editor and upload by hand, but then you are juggling copies. Keeping the change inside FTP by Zaweba means the file you edit is the file that goes up, the conflict warnings still apply before anything overwrites a newer server copy, and the previous version is on your PC if you change your mind.