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 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.

RegExp – validate US phone

A regular expression to validate US phone numbers. Works in all languages I’ve tested.

RegExp – validate email

Here’s a regexp that validates email addresses – this is the most robust pattern I’ve seen, should work against any valid email.

Not sure why, but it fails in AS – at some point I’ll go through the whole thing and modify it to work with ActionScript, but for now I use a much […]

JS/RegExp – js to css properties and back

camel-case to hyphenation, and back (e.g., “borderLeft” to “border-left” and vice-versa).