| Home | Convert and Paste(on SourceForge) | PMT Reconciler | Blog |
Convert and Paste is an open source (GPL) add-on for the Firefox web browser. I created Convert and Paste because I was sick of having to convert backward slashes from Windows file paths to forward slashes when putting up links to files in my company's internal wiki.
Initially, I created a simple add-on that would just convert slashes, but then I thought it might be useful to make the conversions as user definable regular expressions so that people could customize the exact functionality of Convert and Paste.
Initally, Convert and Paste comes ready to convert backward slashes to forward slashes in any string in the clipboard by either selecting "Edit -> Convert and Paste" from the menu, or by pressing Ctrl-Shift-V. The add-on will not modify the clipboard, it just applies the regular expressions and replacements to the string before it pastes it to the currently focused item in Firefox.
You aren't restricted to just replacing slashes, however. Regular expressions are a powerful way for you to work with strings. In fact, there are whole books and sites devoted to regular expressions. By default, the add-on comes configured with this conversion item which you can see by going to "Tools -> Options" on the menu and clicking on the "Convert and Replace" tab in the options dialog:
Before delving into making your own replacements, let's examine the default. The
Regular Expression Replacement /\\/g/
/\\/g part is a regular expression. The part between the forward slashes specifies a pattern to match against a string. The \\ part matches a single backslash in a string. There are two backslashes because backslashes have a special meaning in regular expressions. Normally, a backslash means "don't treat the character after the backslash as a special character". So, if we want to match a literal backslash, then we need to make sure that the regular expression system doesn't ignore the next character's special meaning, which, in our case, would be the forward slash which signals the regular expression engine that we are done specifying a regular expression. The process is known as "escaping" a special character. The g at the end of the regular expression tells the plugin to replace all instances of a backslash that it finds. If we didn't specify this flag, then the replacement would only occur on the first backslash that was found. The replacement part is just a simple string to replace whatever is matched by the regular expression. So in summary, the conversion item says, "Replace all instances of a single backwards slash with a forward slash".
For those familar with regular expressions and (ECMA/Java)script, you will realize that the add-on is just using the replace method on the String object in (ECMA/Java)script.
Here are some interesting and useful codes collected so far:
| Regular Expression | Replacement | Explanation |
|---|---|---|
| /\\/g | / | Replace all back slashes with forward slashes, then put "http:" in front of the entire string. Useful for converting file paths from Windows to links |
| /^/ | http: |