PHP – RegexRouter

PHP Routing class that translates any valid PCRE into a closure, in 14 lines of wholesome goodness For the impatient: class RegexRouter { private $routes = array(); public function route($pattern, $callback) { $this->routes[$pattern] = $callback; } public function execute($uri) { foreach ($this->routes as $pattern => $callback) { if (preg_match($pattern, $uri, $params) === 1) { array_shift($params); […]

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

To convert hyphenated strings to camel case (to convert css “background-image” to js “backgroundImage”): var string = ‘background-image’; string.replace(/\-([a-z])/gi, function (match, hyphenated) { return hyphenated.toUpperCase(); }); This will also handle vendor prefixed properties appropriately… “-webkit-box-shadow” becomes “WebkitBoxShadow”. For pascal case (where first letter is always capitalized), use this: var string = ‘background-image’; string.replace(/(\-|^)([a-z])/gi, function (match, […]

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): $html = preg_replace(‘/([a-z]+\:\/\/[a-z0-9\-\.]+\.[a-z]+(:[a-z0-9]*)?\/?([a-z0-9\-\._\:\?\,\’\/\\\+&%\$#\=~])*[^\.\,\)\(\s])/i’, $string); The more difficult part […]

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: preg_replace(‘/(.{80})/’, "$1\n", $str); 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. /(/*(s*|.*?)**/)|(//.*)/

RegExp – validate US Zip

Simple regular expression to validate US zip codes. /^\d{5}(-\d{4})?$/