Applying Masks Using Regular Expressions In Javascript

Posted August 1, 2006 at 8:43 AM by Ben Nadel

Tags: Javascript / DHTML

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.




Reader Comments

Sep 8, 2006 at 1:54 PM // reply »
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?


Sep 8, 2006 at 2:26 PM // reply »
10,640 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/


Jan 31, 2007 at 10:33 PM // reply »
168 Comments

Ben, this rocks! Thanks for sharing.


Feb 1, 2007 at 7:57 AM // reply »
10,640 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.


Jul 17, 2007 at 6:40 PM // reply »
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() ],


Jul 18, 2007 at 7:28 AM // reply »
10,640 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 :)


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »