| Christopher Bradford |
Regular Expression Search and Replace. Javascript that uses the new InputBox and MessageBox methods of beta 2 to provide you with regular expression search and replace (with full backreferencing capabilities). It uses the Javascript regular expressions engine.
Some of the features of this script:
- Allows you to search and replace in currently selected text. Yaay! It detects whether you have text selected; if so, you are given the option to restrict the operation to selected text only.
- Can also replace in the entire document if you have nothing selected or if you choose not to restrict the operation to the selected text.
- Supports full backreferencing, using \1..\9 in the search expression and $1..$9 in the replace expression.
- Takes advantage of the Javascript regular expression engine to provide a familiar (for most) regular expression syntax (\t for tab, \n for newline, [^<expression>] for negation, and so on).
- Does some validation of your RE. This is quite minimal right now; I plan to enhance it in future versions. It basically just checks to make sure all the necessary elements are present.
- Quite fast. I tested it on a 15,000-line document, and the replace was completed in about 7 seconds on my P233. I don't think you will find it slow, as most people work with much smaller files than that.
- Has the option to automatically adjust for an idiosyncracy of HomeSite's: The . character usually doesn't match newlines, but in HomeSite, for some reason, it does. This script can adjust your RE for you to compensate for this. If you include the .* construction in your RE, you will be asked if you want the RE adjusted automatically. Of course, it's better for you to do it, as the script can only do so much with guessing what you are looking for.
This version of the script is somewhat limited (I'm working on an upgraded version):
- It will only work on the current document (you'll need to bind the script to a custom button - I'm planning to do a nice icon, but if anyone wants to contribute one...) In the near future I will add the functionality to replace in all open documents or all documents in a folder, or maybe even let you make a list of documents to replace on...
- There is no Undo function if you replace on the whole document. I think this is a bug in HomeSite that will be fixed.
- No backups are made, in case you change your mind or make a mistake. This will be coming soon...
- No way to look at your original RE while composing the replacement string. This will be coming soon, too.
- Some incorrectly formed REs will crash the script. Nothing serious, the script will just come up in HomeSite with the "offending" line highlighted. It's probably your RE. Check it very closely, and check out the JScript regexp docs at Microsoft's scripting site (www.microsoft.com/scripting). There may be a couple of funny things with the RE engine...
- No real help documentation. I'm working on this, and you may get the option to view help before doing an RE Search and Replace.
I'm very open to suggestions for improvements, criticisms, anything to make this script better. It's something a lot of people have been asking for, and I hope you find it useful. Requires HS 4 beta 2 or above.
|