Use Regular Expressions in TextExpander via JavaScript

Use regular expressions on your Mac, iPad, and iPhone with TextExpander JavaScript snippets. This is specifically for TextExpander 5 and TextExpander touch 3.5 and later.

Regular Expressions can be incredibly useful when searching and replacing patterns in text. Blogger and podcaster Jason Snell writes that regular expressions saved him “hundreds of hours of drudgery”.

Let’s say that you frequently copy URLs to the clipboard and that these URLs have a bunch of tracking junk at the end in the form of a query string. You want to strip that junk. This is the perfect job for a regular expression, because there’s a pattern. You want to strip everything from the question mark (?) to the closing quotation mark, if present. Here’s an example of what you’ve got:

Here’s what you want:

For fun, let’s also handle the case when you’ve got the URL in HTML format, such as:
<a href=”″>Hello</a>

1. Make a new snippet in TextExpander, and set the Content: popup to JavaScript, then enter this as the snippet content:

// Start with the clipboard content
var result = TextExpander.pasteboardText;

// Strip the HTML query; everything after the ? up to
// any closing quotation mark
result = result.replace(/\?[^\"]+/, "");

// If we've got HTML (<a href…), strip that too
var hrefRegex = /<\s*a\s+href\s*=\s*\"([^\"]+)\"\s*>[^<]+<\s*\/a\s*>/i;
var matches = hrefRegex.exec(result);
if (matches && matches.length > 0) {
	result = matches[1];

// Return the result

2. Set an abbreviation, then copy the link above to your clipboard. Enter your abbreviation, and voilà, you get your stripped URL.

I use a JavaScript regular expression to take text lists such as this:

- Suggested snippets exclude most single dictionary words
- Adds new notification preference for snippet suggestions
- Fixes secure input notifications for Chrome
- Other minor fixes and improvements

…and turn them into HTML list items:

   <li>Suggested snippets exclude most single dictionary words</li>
   <li>Adds new notification preference for snippet suggestions</li>
   <li>Fixes secure input notifications for Chrome</li>
   <li>Other minor fixes and improvements</li>

Here’s the snippet I use to do that:

// Start with the clipboard content
var result = TextExpander.pasteboardText;

// Replace all lines which start with "- " with "\t<li>"
result = result.replace(/^- /gm, "\t<li>");

// Replace all newlines with "</li>\n"
result = result.replace(/\n/g, "</li>\n");

I use regular expressions to save myself a ton of time on repetitive text manipulation. Regular expressions and TextExpander are a fantastic combination. Here are some resources if you’re interested in learning more:
Regular Expressions Tutorial: A good place to get started
JavaScript Regex Cheatsheet: A concise reference to regular expressions in JavaScript
Patterns – The Regex App: Handy app for building and testing regular expressions
Mastering Regular Expressions: Definitive reference on regular expressions

View this article on our Blog

Category: All posts, TextExpander, TextExpander Tips,