PHP – RegexRouter

PHP Routing class that translates any valid PCRE into a closure, in 14 lines of wholesome goodness For the impatient:

JavaScript – hyphenated (css) to camelcase (or pascal) regexp

To convert hyphenated strings to camel case (to convert css “background-image” to js “backgroundImage”):

This will also handle vendor prefixed properties appropriately… “-webkit-box-shadow” becomes “WebkitBoxShadow”. For pascal case (where first letter is always capitalized), use this:

JavaScript Validation Class

I’m going to go ahead and post this because I keep referencing it personally, but with the caveat that it’s not been well tested or documented whatsoever. Right now it’s just a bunch of boolean returns on strings (generally user input) – I’m considering adding numeric- and date-centric validation but am not sure yet. If […]

PHP – really simple linkify

we all know that it’s impossible to perfectly match URLs from human input… but i spent a little time compiling various solutions on the boards and have ended up with this (note – this is largely pieced together from other sources – I’m not claiming original work):

The more difficult part was differentiating what […]

JavaScript RegExp to remove comments

UPDATE 02/16/13 As the gentleman in the comments pointed out, it’s (practically) impossible to use a single regular expression to appropriately remove all comments from javascript (or any construct with strings), and I’d assume virtually impossible to do so even with a series of patterns. I suspect a full-fledged (but simple) parser is required, so […]

PHP + RegExp: Adding line breaks to dynamic text

When outputting user- (or client-) supplied content from a DB, you’ll often have very long line runs. For readability, you can easily break those into bit-sized lines:

The above will insert a line break into $str at every 80th character. I’ll update this to account for content containing markup at some point.

JS/AS – Cache your RegExp!

In both JavaScript and ActionScript, creating a regular expression – even using literal notation – results in the instantiation of a new RegExp object. If you’re going to use a pattern repeatedly (which is usually the case) – cache your patterns! Just like you would any other object – “literal” doesn’t mean “free”. Simple test […]

JS/AS Date Object from MySQL DATE or DATETIME

The ECMAScript Date object accepts a string that it pretty generously tries to convert to a valid date. However, DATE and DATETIME returns from MySQL are formatted Y-M-D, which will cause new Date(str) or Date.parse(str) to barf. The quick fix is just to use a simple RegExp to move the year to the 3rd position. […]

RegExp – match JS/AS/PHP/CSS comments

The following pattern matches comments used in JavaScript, ActionScript, PHP, CSS, etc (/* comments */, and // comments), greedily.

RegExp – validate US Zip

Simple regular expression to validate US zip codes.