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.
Four Easy and Useful Regular Expression
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.
This can be customized by adding or removing any punctuation or special characters.
3. All Punctuation
I used this to save all punctuation from a string before splitting that string at punctuation.
4. Finding a link
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.