Skip to main content
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Henry Hollenstain
Ben Nadel at InVision In Real Life (IRL) 2018 (Hollywood, CA) with: Henry Hollenstain ( @hhollenstain )

Applying Masks Using Regular Expressions In Javascript

By on

I have blogged several times about using regular expressions (RegExp) in Javascript. I think regular expressions are absolutely wonderful. And, one of the great things about Javascript regular expressions (as opposed to reg ex in other languages) is that you can pass the individual groups matches to a function call:

strValue.replace(
	new RegExp( "([0-9]+)", "gi" ),

	// Pass a function as the "replace" argument for
	// the regular expression.
	function( $1 ){
		// Return the value you want to replace into
		// the string.
		return( "digit" );
	});

This is a really cool feature, but it was not until recently that I came to see the amazing POWER of this feature. I am working on a prototype library of useful functions such as string trimming and date formatting. Date and time formatting, as you might know from ColdFusion, takes a mask argument to define how the date should be formatted. For example, the mask "mm/dd/yyyy" would return the string "08/01/2006".

There are many different combinations of masks available to the user. As it turns out, using this regular expression flexibility, it actually makes applying a mask extremely easy. To explore this, let's take a look at my formatDate() function:

(new Date()).formatDate( "mmm d, yyyy" );

The first thing I do inside the formatDate() function is define the values for each part of the potential mask:

// Create the values for each part of the potential date mask.
var objParts = {
	"d": this.getDate(),
	"dd": (this.getDate().toString().length == 1) ? ("0" + this.getDate()) : this.getDate(),
	"ddd": [ "Sun","Mon","Tue","Wed","Thr","Fri","Sat" ][ this.getDay() ],
	"dddd": [ "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" ][ this.getDay() ],
	"m": this.getMonth(),
	"mm": (this.getMonth().toString().length == 1) ? ("0" + this.getMonth()) : this.getMonth(),
	"mmm": [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ][ this.getMonth() + 1 ],
	"mmmm": [ "January","February","March","April","May","June","July","August","September","October","November","December" ][ this.getMonth() + 1 ],
	"yy": this.getYear().toString().substring( 1, 3 ),
	"yyyy": this.getFullYear()
	}

Each part of the object (equivalent to a ColdFusion struct), objPart, is a possible part of the date mask. Now, assuming that the passed argument "strMask" holds the user's desired date format, we could apply the mask to the date using regular expression:

// There was no special date formatting, so just use the mask.
return(
	strMask.replace(
		// Set up the regular expression for possible mask parts.
		new RegExp( "(d{1,4}|m{1,4}|y{4}|y{2})", "gi" ),

		// Have each match get passed to this function.
		function( $1 ){
			// Return the equivalent mask part as it applies to the
			// date object.
			return( objParts[ $1 ] );
			})
	);

As you can see, the regular expression (d{1,4}|m{1,4}|y{4}|y{2}) will simply match any possible part of the available date mask options. Each matching part is then passed to the function we defined as the second argument to the replace() method. This function merely returns the value of the objParts[] object using the matched group as the key. Take a second to sit back and be in awe of the amazing flexibility that this allows you.

Putting it all together:

// Formats the date with the given date mask. The mask is returned
// and the internal date is not altered.
Date.prototype.formatDate = function( strMask ){
	// Create the values for each part of the potential date mask.
	var objParts = {
		"d": this.getDate(),
		"dd": (this.getDate().toString().length == 1) ? ("0" + this.getDate()) : this.getDate(),
		"ddd": [ "Sun","Mon","Tue","Wed","Thr","Fri","Sat" ][ this.getDay() ],
		"dddd": [ "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" ][ this.getDay() ],
		"m": this.getMonth(),
		"mm": (this.getMonth().toString().length == 1) ? ("0" + this.getMonth()) : this.getMonth(),
		"mmm": [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ][ this.getMonth() + 1 ],
		"mmmm": [ "January","February","March","April","May","June","July","August","September","October","November","December" ][ this.getMonth() + 1 ],
		"yy": this.getYear().toString().substring( 1, 3 ),
		"yyyy": this.getFullYear()
		}

	// Check to see if we have special date formatting options.
	switch ( strMask ){
		case "short":
			return( objParts[ "m" ] + "/" + objParts[ "d" ] + "/" + objParts[ "yyyy" ] );
			break;

		case "medium":
			return( objParts[ "mmm" ] + " " + objParts[ "d" ] + ", " + objParts[ "yyyy" ] );
			break;

		case "long":
			return( objParts[ "mmmm" ] + " " + objParts[ "d" ] + ", " + objParts[ "yyyy" ] );
			break;

		case "full":
			return( objParts[ "dddd" ] + ", " + objParts[ "mmmm" ] + " " + objParts[ "d" ] + ", " + objParts[ "yyyy" ] );
			break;

		default:
			// There was no special date formatting, so just use the mask.
			return(
				strMask.replace(
					new RegExp( "(d{1,4}|m{1,4}|y{4}|y{2})", "gi" ),
					function( $1 ){
						return( objParts[ $1 ] );
						})
				);
			break;
	}
}

Can you imagine trying to do this without regular expressions? Let's take a look at an example that doesn't use regular expression. This example was taken off of the Javascript Tool Box:

function formatDate(date,format){format=format+"";var result="";var i_format=0;var c="";var token="";var y=date.getYear()+"";var M=date.getMonth()+1;var d=date.getDate();var E=date.getDay();var H=date.getHours();var m=date.getMinutes();var s=date.getSeconds();var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;var value=new Object();if(y.length < 4){y=""+(y-0+1900);}value["y"]=""+y;value["yyyy"]=y;value["yy"]=y.substring(2,4);value["M"]=M;value["MM"]=LZ(M);value["MMM"]=MONTH_NAMES[M-1];value["NNN"]=MONTH_NAMES[M+11];value["d"]=d;value["dd"]=LZ(d);value["E"]=DAY_NAMES[E+7];value["EE"]=DAY_NAMES[E];value["H"]=H;value["HH"]=LZ(H);if(H==0){value["h"]=12;}else if(H>12){value["h"]=H-12;}else{value["h"]=H;}value["hh"]=LZ(value["h"]);if(H>11){value["K"]=H-12;}else{value["K"]=H;}value["k"]=H+1;value["KK"]=LZ(value["K"]);value["kk"]=LZ(value["k"]);if(H > 11){value["a"]="PM";}else{value["a"]="AM";}value["m"]=m;value["mm"]=LZ(m);value["s"]=s;value["ss"]=LZ(s);while(i_format < format.length){c=format.charAt(i_format);token="";while((format.charAt(i_format)==c) &&(i_format < format.length)){token += format.charAt(i_format++);}if(value[token] != null){result=result + value[token];}else{result=result + token;}}return result;}

While the code is hard to read (I wasn't about to take the time to parse it), you can clearly see that there are multiple WHILE loops. And within each of those while loops they are getting tokens, performing logic, checking for null values. All a complete waste of time. Let Javascript perform the heavy lifting for you.

Are you beginning to see the possibilities? Regular expressions are so freakin' powerful it almost makes my ears bleed to think about it.

Want to use code from this post? Check out the license.

Reader Comments

1 Comments

This looks pretty nice, but do you have an example of implementation. I'm not familiar with this particular syntax (relatively new to js). How do you call this exactly?

15,674 Comments

Dylan,

I have thrown together a little demo of how this works, take a look, especially at the source code. It's a bit complicated. It basically takes the date object and prototypes two format functions, dateFormat() and timeFormat().

http://www.bennadel.com/resources/demo/2/

15,674 Comments

No problem dude. Please let me know if you run into any Javascript road blocks that you need help with. I love me some Javascript.

1 Comments

The indexes into the month names and the number of the month look to be flipped around.

I think the lines

# "m": this.getMonth(),
# "mm": (this.getMonth().toString().length == 1) ? ("0" + this.getMonth()) : this.getMonth(),
# "mmm": [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ][ this.getMonth() + 1 ],
# "mmmm": [ "January","February","March","April","May","June","July","August","September","October","November","December" ][ this.getMonth() + 1 ],

should be

# "m": this.getMonth() + 1,
# "mm": (this.getMonth().toString().length == 1) ? ("0" + this.getMonth()) : this.getMonth() + 1,
# "mmm": [ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" ][ this.getMonth() ],
# "mmmm": [ "January","February","March","April","May","June","July","August","September","October","November","December" ][ this.getMonth() ],

15,674 Comments

It is possible. I go back and forth between ColdFusion (1-based arrays) and Javascript (0-based arrays) that I may have just messed it up. However, I am pretty sure I tested this stuff, but it's possible that I was so excited about the regular expressions that I didn't notice it was the wrong month :)

1 Comments

"mm": (this.getMonth().toString().length == 1) ? ("0" + this.getMonth()) : this.getMonth() + 1

should be

"mm": ((this.getMonth() +1).toString().length == 1) ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1)

To properly handle 'October'

1 Comments

I want to convert a string containing numbers only, to $format. for example the input string 250000 needs to be converted to $2,50,000.
Kindly keep me posted with your solutions.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel