Using Regular Expressions to Convert Text to “Cat Mode”

Question: How can I convert normal text to “cat mode”? If a visitor clicks the “cat mode” link, the text reloads, replacing all words with “meow,” except for links. I also want to maintain punctuation and capitalization.

Answer: Essentially, I used jQuery to create a page that would dynamically load text.

While building out the feature, I created a few helper functions–isPunct, isLink, and isWords–that would return boolean responses and help determine whether to leave a text segment in its current state or convert it to meows using my meowify method.

This lead me to revisit Regular Expressions, a programming tool I’ve sometimes been wary to use because–let’s face it–compared to the readability of Ruby, Regular Expressions are hieroglyphic. In reality, though, they’re not that tricky to use, and sometimes extremely helpful.

Today, I checked out Britt Ballard’s excellent blog post about Regular Expressions, and was inspired to clean up the Regular Expressions I used in my functions, for practice and perhaps future reference. My project didn’t require particularly complex expressions, so be aware that there’s much more that can be done with Regular Expressions than I’m covering here. (Check out Britt’s post for some examples, keeping in mind that his use Ruby while mine were incorporated into a Javascript project.)

Four Easy and Useful Regular Expression

  1. Letters
JS letters
Return an array of all letters, case insensitive.

The [a-z] in this example could be replaced with any string or letters. Removing the “i” from the statement would make it case sensitive, so that, in this example, only [“y”, “a”] would be returned. Removing the “g” from the statement would mean only the first match would be returned.

2. Punctuation

JS match punct
Return the first punctuation found in a string.

This can be customized by adding or removing any punctuation or special characters.

3. All Punctuation

JS globally match punct
Return an array of all punctuation in a string.

I used this to save all punctuation from a string before splitting that string at punctuation.

4. Finding a link

JS match index
Return the starting index of a match in a string.

This could be used to find the start of a link in a string of text.

Using these regular expressions I was able to majorly simplify my code (though I’m finding that trying to convert my sentences while maintaining proper spacing and formatting is still quite a challenge!) I’ve still got some work ahead of me with the site, but I’m hoping that the work I’m putting in to automate my cat mode conversion will be helpful in later parts of my project as well.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s